0

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

4

1 回答 1

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 元素中使用点(“.”)符号形式来指示嵌入属性中的属性。与点表示法一起使用的每个标识符的值是相应嵌入字段或属性的名称。

于 2014-01-27T18:40:23.900 回答