J.H
J.H

Reputation: 133

Hibernate @OneToMany inside @Embeddable

I have the following scenario:

@Entity
class A {
    @ElementCollection
    private Set<B> setOfB;
}

@Embeddable
class B{
    @OneToMany
    private Set<C> setOfC;
}

@Entity
class C{
    private String name;
}

Following this question looks like this is doable, however I am getting the following errors. Any ideas what am I doing wrong?

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: A_setOfB, for columns: [org.hibernate.mapping.Column(setOfB.setOfC)]

Thanks.

Upvotes: 1

Views: 2241

Answers (1)

K.Nicholas
K.Nicholas

Reputation: 11551

It isn't possible in Hibernate according to their manual at 7.2.3. Collections of basic types and embeddable objects.

But you are not limited to basic types, the collection type can be any embeddable object. To override the columns of the embeddable object in the collection table, use the @AttributeOverride annotation.

@Entity
public class User {
   [...]
   public String getLastname() { ...}

   @ElementCollection
   @CollectionTable(name="Addresses", joinColumns=@JoinColumn    (name="user_id"))
   @AttributeOverrides({
      @AttributeOverride(name="street1", column=@Column(name="fld_street"))
   })
   public Set<Address> getAddresses() { ... } 
}

@Embeddable
public class Address {
    public String getStreet1() {...}
    [...]
}

Such an embeddable object cannot contains a collection itself.

Upvotes: 2

Related Questions