Question

I have a situation with these classes, where 1st is contained by 2nd as @Embedded field, and then 3rd is containing 2nd two times as two different @Embedded field :

@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;

And when I try to run the code with classes above I get the exception

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

It's the same when I substitute "size" with "storage" in @AttributeOverride.

Any idea how to pull model like this one? If possible I would avoid creating new entities and keep both StorageSize and StorageSizeTBPerMonth as embeddable classes.

Was it helpful?

Solution

There is no attribute size to override in StorageSizeTBPerMonth. It does have embedded field storage which holds attribute size. That's why storage.size is way to go:

@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;

In JPA 2.0 specification this is told with following words:

To override mappings at multiple levels of embedding, a dot (".") notation form must be used in the name element to indicate an attribute within an embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top