Annotations of nested Embeddables are not processed when the Embeddables are in an ElementCollection.
We have the following situation:
EmbeddableOne includes EmbeddableTwo includes EmbeddableThree. Entity includes a single EmbeddableOne and a Set of EmbeddableOnes using ElementCollection.
{code:java} public class EmEntity { @Id @GeneratedValue private long id; private String name;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate") private LocalDate birthdate;
@Embedded private EmEmbeddableOne one;
@ElementCollection @CollectionTable(name = "EM_ENTITY_ONES") @AttributeOverrides({ @AttributeOverride(name = "date", column = @Column(name = "OV_DATE_ONE")), @AttributeOverride(name = "two.date", column = @Column(name = "OV_DATE_TWO")), @AttributeOverride(name = "two.three.date", column = @Column(name = "OV_DATE_THREE")),
@AttributeOverride(name = "name", column = @Column(name = "OV_NAME_ONE")), @AttributeOverride(name = "two.name", column = @Column(name = "OV_NAME_TWO")), @AttributeOverride(name = "two.three.name", column = @Column(name = "OV_NAME_THREE")), }) private Set<EmEmbeddableOne> manyOnes = new HashSet<>(); ... {code}
Each Embeddable has it's own @Column and @Type Annotation:
{code:java} @Embeddable public class EmEmbeddableOne { @Column(name = "NAME_ONE") private String name;
@Column(name = "DATE_ONE") @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate") private LocalDate date;
@Embedded private EmEmbeddableTwo two; ... {code}
When creating the tables Hibernate correctly uses the Annotations on the Properties to name the Columns and to set the Types of the Columns (using MySql):
create table EM_ENTITY (id bigint not null, birthdate date, name varchar(255), DATE_ONE date, NAME_ONE varchar(255), DATE_TWO date, NAME_TWO varchar(255), DATE_THREE date, NAME_THREE varchar(255), primary key (id))
When creating the table for the ElementCollection (*without *AttributeOverrides):
{code:java} @ElementCollection @CollectionTable(name = "EM_ENTITY_ONES") // @AttributeOverrides({ // @AttributeOverride(name = "date", column = @Column(name = "OV_DATE_ONE")), // @AttributeOverride(name = "two.date", column = @Column(name = "OV_DATE_TWO")), // @AttributeOverride(name = "two.three.date", column = @Column(name = "OV_DATE_THREE")), // // @AttributeOverride(name = "name", column = @Column(name = "OV_NAME_ONE")), // @AttributeOverride(name = "two.name", column = @Column(name = "OV_NAME_TWO")), // @AttributeOverride(name = "two.three.name", column = @Column(name = "OV_NAME_THREE")), // }) private Set<EmEmbeddableOne> manyOnes = new HashSet<>(); ... {code} I get the following MappingException: Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: nestedEmbeddables.entity.EmEntity.manyOnes column: date at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:333) at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:358) at org.hibernate.mapping.Collection.validate(Collection.java:319) at org.hibernate.mapping.Set.validate(Set.java:27) at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:333) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:464) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881) ... 26 more
although a different Column-Name is defined for each Embeddable.
When creating the table for the ElementCollection (with AttributeOverrides enabled) the MappingException is gone but the Date-Columns have a *wrong Type* (tinyblob instead of date):
create table EM_ENTITY_ONES (EmEntity_id bigint not null, OV_DATE_ONE date, OV_NAME_ONE varchar(255), OV_DATE_TWO tinyblob, OV_NAME_TWO varchar(255), OV_DATE_THREE tinyblob, OV_NAME_THREE varchar(255)) |
|