問題描述
有人可以向我解釋一下休眠中的 @MapsId
嗎?我很難理解它.
Can someone please explain to me @MapsId
in hibernate? I'm having a hard time understanding it.
如果可以用一個(gè)例子來解釋它會(huì)很棒,它最適用于什么樣的用例?
It would be great if one could explain it with an example and in what kind of use cases is it most applicable?
推薦答案
這里有一個(gè)很好的解釋,來自 對(duì)象數(shù)據(jù)庫.
Here is a nice explanation from Object DB.
指定為 EmbeddedId 主鍵、EmbeddedId 主鍵中的屬性或父實(shí)體的簡單主鍵提供映射的 ManyToOne 或 OneToOne 關(guān)系屬性.value 元素指定關(guān)系屬性對(duì)應(yīng)的復(fù)合鍵中的屬性.如果實(shí)體的主鍵與關(guān)系引用的實(shí)體的主鍵的Java類型相同,則不指定value屬性.
Designates a ManyToOne or OneToOne relationship attribute that provides the mapping for an EmbeddedId primary key, an attribute within an EmbeddedId primary key, or a simple primary key of the parent entity. The value element specifies the attribute within a composite key to which the relationship attribute corresponds. If the entity's primary key is of the same Java type as the primary key of the entity referenced by the relationship, the value attribute is not specified.
// parent entity has simple primary key
@Entity
public class Employee {
@Id long empId;
String name;
...
}
// dependent entity uses EmbeddedId for composite key
@Embeddable
public class DependentId {
String name;
long empid; // corresponds to primary key type of Employee
}
@Entity
public class Dependent {
@EmbeddedId DependentId id;
...
@MapsId("empid") // maps the empid attribute of embedded id
@ManyToOne Employee emp;
}
閱讀 API 文檔 這里.
這篇關(guān)于有人可以在休眠中向我解釋@MapsId嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!