【发布时间】:2013-12-13 03:28:17
【问题描述】:
在我看来,以下两种映射方式几乎没有区别。这是一个基于@MapsId javadoc 的示例:
// parent entity has simple primary key
@Entity
public class Employee {
@Id long empId;
...
}
// 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;
}
如果我将 Dependent 的映射更改为:
@Entity
public class Dependent {
@EmbeddedId DependentId id;
@ManyToOne
@JoinColumn("empid", insertable=false, updatable=false)
Employee emp;
}
以上两种方式有什么区别?
【问题讨论】:
-
你测试了吗?
-
是的,但还有一些其他奇怪的问题。但是根据文档的描述,我找不到它们的任何区别。可以给点提示吗?
-
那么什么对你更好?我曾经使用 JoinColumn,对我来说它有效。但是现在我看到了 MapsId,所以我也想知道它是如何工作的;)
-
@Rafik991 我也一样 :P 我曾经使用 JoinColumn 但我看到了 MapsId(也许它是 JPA2 的新东西?或者我过去错过了它)。这个问题的原因只是为了了解区别:P