问题描述
我创建了一个使用@Id指向@Embeddable复合键的实体。我相信我所做的一切都很好。然而,将@Id切换到@EmbeddedId之后,就我所知,一切都可以继续正常工作。
之前:
@Entity
public final class MyEntity {
private CompoundKey id;
@Id
public CompoundKey getId(){
return id;
}
public void setId(CompoundKey id){
this.id = id;
}
后:
@Entity
public final class MyEntity {
private CompoundKey id;
@EmbeddedId
public CompoundKey getId(){
return id;
}
public void setId(CompoundKey id){
this.id = id;
}
使用@Id和@EmbeddedId注释引用复合键?
实际上,根据规范,映射 Embeddable
复合键的正确方法是在<版本之后的。引用JPA 1.0规范:
$ b
后来:
I've created an entity that uses @Id to point to an @Embeddable compound key. Everything I believe works fine as is. However, after switching @Id to @EmbeddedId everything continues to work fine as far as I can tell.
Before:
@Entity
public final class MyEntity {
private CompoundKey id;
@Id
public CompoundKey getId() {
return id;
}
public void setId(CompoundKey id) {
this.id = id;
}
After:
@Entity
public final class MyEntity {
private CompoundKey id;
@EmbeddedId
public CompoundKey getId() {
return id;
}
public void setId(CompoundKey id) {
this.id = id;
}
Is there a difference between using the @Id and @EmbeddedId annotations when referencing a compound key?
I'm actually surprised the "before" version is working. According to the specification, the correct way to map your Embeddable
compound key is the "after" version. Quoting the JPA 1.0 specification:
And later:
这篇关于使用@Id和@EmbeddedId来区分复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!