有人可以向我解释一下 hibernate 中的 @MapsId
吗?我很难理解它。
如果可以用一个例子来解释它会很棒,它最适用于什么样的用例?
最佳答案
这是来自 Object DB 的一个很好的解释.
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 Docs在这里。
关于java - 有人可以在 hibernate 中向我解释@MapsId吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923643/