3

I have an entity with an optional sub-entity containing more fields. The sub-entity again has a collection of elements.

Is this generally impossible with Hibernate or am I missing something?

@Entity @Table(name="my_entity")
public class MyEntity {
  @Id 
  private Long id;

  @Embedded 
  @JoinTable(name="..." joinColumns=@JoinColumn(name="myEntityId"))
  private OptionalFields optionalFields;
}

@Embeddable
public class OptionalFields {
   @ElementCollection
   @CollectionTable(name="...", joinColumns=@JoinColumn(name="myEntityId"))
   private List<OpeningHoursItem> openingHours;
}

@Embeddable
public class OpeningHoursItem {

}

Error message is:

Could not determine type for: java.util.List, at table: my_entity, for columns: [org.hibernate.mapping.Column(opening_hours)]
Bastian Voigt
  • 5,311
  • 6
  • 47
  • 65
  • 2
    Does [this](http://stackoverflow.com/questions/22126397/embeddable-and-elementcollection-nesting) answer your question? – Jan Mar 04 '15 at 16:25
  • Thanks @Jan, interesting link. It is similar but not quite the same, since I have cardinality 1:1:N whereas your link has 1:M:N. – Bastian Voigt Mar 05 '15 at 08:32
  • I am not able to reproduce the error. If I only add any field to OpeningHoursItem and replace name='...' with proper names - it works. – wypieprz Mar 05 '15 at 11:11
  • @wypieprz O RLY??? WTF. What version of Hibernate are you using? Mine is 4.3.6.Final – Bastian Voigt Mar 05 '15 at 11:19
  • 4.3.5.Final (hibernate-core + hibernate-entitymanager + hibernate-jpamodelgen + hibernate-c3p0) and 5.1.0.Final (hibernate-validator) – wypieprz Mar 05 '15 at 11:23
  • Maybe I stripped down the sample code too much. No idea. Or maybe it's time for vacation? – Bastian Voigt Mar 05 '15 at 12:49
  • When I place the ElementCollection and CollectionTable annotations on the getter, it works. On the field it doesn't. TIME FOR VACATION! – Bastian Voigt Mar 05 '15 at 13:07

1 Answers1

0

It may sound strange, but it really works:

Move all your annotations from the fields to the getters, then the error goes away.

Bastian Voigt
  • 5,311
  • 6
  • 47
  • 65