如何在@Embeddable 中使用@GeneratedValue 作为复合主键?
How to use @GeneratedValue inside @Embeddable used as composite primary key?
我的数据库中有一个 table 和主复合键,当我使用 Hibernate 对其建模时,我使用 @EmbeddedId
和 @Embedable
。此复合主键的一列具有 @GeneratedValue(strategy = GenerationType.IDENTITY)
.
的生成值
当我尝试在我的数据库中创建两个新对象时,出现如下错误:
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [MyPackage#MyEmbedableClass [MyGeneratedPrimaryKeyValue=null, OtherPrimaryKey=21]]
但是当我查看我的数据库时,我的对象已经创建了。我不明白我的错误,我不知道如何解决我的问题。
我找到了一些类似我的主题,但我还没有找到问题的答案。
-
second exemple
@Entity(name = "Certification")
@Table(name = "CERTIFICATION")
public class Certification implements Serializable {
static final long serialVersionUID = -4399907743392740963L;
@EmbeddedId
private CertificationPK certificationPK;
// Others variables
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
@Embeddable
public class CertificationPK implements Serializable {
private static final long serialVersionUID = 1433990897506209802L;
// MyGeneratedPrimaryKeyValue=null when I create
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull
@Column(name = "CERTIFICATION_ID")
private Integer certificationId;
// Other variable
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
在此先感谢您的帮助。
看来您不能将 @GeneratedValue
与 @EmbeddedId
一起使用。您可以在 this article 中找到一些解决方法,但它对我来说效果不佳。
如this section of documentation, it should be possible to use @GeneratedValue
with @IdClass
. But, it does not work due to HHH-9662中所述。
正如文档的 this 部分所述:
When using composite identifiers, the underlying identifier properties must be manually assigned by the user.
Automatically generated properties are not supported to be used to generate the value of an underlying property that makes the composite identifier.
Therefore, you cannot use any of the automatic property generator described by the generated properties section like @Generated
, @CreationTimestamp
or @ValueGenerationType
or database-generated values.
Nevertheless, you can still generate the identifier properties prior to constructing the composite identifier.
我的数据库中有一个 table 和主复合键,当我使用 Hibernate 对其建模时,我使用 @EmbeddedId
和 @Embedable
。此复合主键的一列具有 @GeneratedValue(strategy = GenerationType.IDENTITY)
.
当我尝试在我的数据库中创建两个新对象时,出现如下错误:
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [MyPackage#MyEmbedableClass [MyGeneratedPrimaryKeyValue=null, OtherPrimaryKey=21]]
但是当我查看我的数据库时,我的对象已经创建了。我不明白我的错误,我不知道如何解决我的问题。
我找到了一些类似我的主题,但我还没有找到问题的答案。
second exemple
@Entity(name = "Certification")
@Table(name = "CERTIFICATION")
public class Certification implements Serializable {
static final long serialVersionUID = -4399907743392740963L;
@EmbeddedId
private CertificationPK certificationPK;
// Others variables
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
@Embeddable
public class CertificationPK implements Serializable {
private static final long serialVersionUID = 1433990897506209802L;
// MyGeneratedPrimaryKeyValue=null when I create
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull
@Column(name = "CERTIFICATION_ID")
private Integer certificationId;
// Other variable
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
在此先感谢您的帮助。
看来您不能将
@GeneratedValue
与@EmbeddedId
一起使用。您可以在 this article 中找到一些解决方法,但它对我来说效果不佳。如this section of documentation, it should be possible to use
@GeneratedValue
with@IdClass
. But, it does not work due to HHH-9662中所述。正如文档的 this 部分所述:
When using composite identifiers, the underlying identifier properties must be manually assigned by the user.
Automatically generated properties are not supported to be used to generate the value of an underlying property that makes the composite identifier.
Therefore, you cannot use any of the automatic property generator described by the generated properties section like
@Generated
,@CreationTimestamp
or@ValueGenerationType
or database-generated values.Nevertheless, you can still generate the identifier properties prior to constructing the composite identifier.