我遇到这些类的情况,其中1st包含在2nd作为@Embedded字段,然后3rd包含2nd作为两个不同的@Embedded字段:
@Embeddable
public class StorageSize {
// ...
@Column(nullable = false)
private Long size;
// ...
@Embeddable
public class StorageSizeTBPerMonth {
// ...
@Embedded
private StorageSize storage = new StorageSize();
// ...
@Entity
public class StorageRange {
// ...
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "size", column = @Column(name ="storage_low")) })
private StorageSizeTBPerMonth limitLow;
// ...
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "size", column = @Column(name = "storage_high")) })
private StorageSizeTBPerMonth limitHigh;
当我尝试使用上面的类运行代码时,我得到了异常
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.mycompany.data.model.StorageRange column: size (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:694)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:694)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
at org.hibernate.mapping.RootClass.validate(RootClass.java:236)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1193)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1378)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:892)
... 24 more
当我在@AttributeOverride中用“storage”替换“size”时,它是一样的。
任何想法如何拉这样的模型?如果可能的话,我会避免创建新的实体,并将StorageSize和StorageSizeTBPerMonth保持为可嵌入的类。
答案 0 :(得分:2)
size
中没有要覆盖的属性StorageSizeTBPerMonth
。它确实有嵌入字段storage
,其中包含属性size
。这就是为什么storage.size
可行的原因:
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "storage.size", column = @Column(name ="storage_low")) })
private StorageSizeTBPerMonth limitLow;
// ...
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "storage.size", column = @Column(name = "storage_high")) })
private StorageSizeTBPerMonth limitHigh;
在JPA 2.0规范中,用以下词语告知:
要覆盖多个嵌入级别的映射,必须在name元素中使用点(“。”)表示形式来指示嵌入属性中的属性。与点表示法一起使用的每个标识符的值是相应嵌入字段或属性的名称。