我有这些类的情况,其中 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 保留为可嵌入类。