Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embeddable entity with @OneToMany attribute

Say, I have following entities:

@Entity
public class A {
  @Id
  @GeneratedValue
  private Long id;

  @Embedded
  private B b;

  //getters and setters
}

@Embeddable
public class B {
  @OneToMany
  private List<C> cList;
  //getters and setters
}

@Entity
public class C {
  @Id
  @GeneratedValue
  private Long id;
  //other fields, getters and setters
}

Using schema-autogeneration feature with Hibernate I get an additional table which contains mappings between A and C. But I'd like to implement a one-to-many relationship by adding A's id into C (e.g without additional table).

Is this possible? If yes, what annotations should I use to create such a mapping?

like image 793
jFrenetic Avatar asked Mar 26 '12 19:03

jFrenetic


1 Answers

In general that is possible with @JoinColumn annotation. It works also with embeddables.

@OneToMany
@JoinColumn(name="A_ID")
private List<C> cList;

If you are not happy with A_ID name for column given in embeddable, you can override column name in entity A:

@AssociationOverride(name= "cList",
        joinColumns = @JoinColumn(name="SOME_NAME_FOR_JOIN_COLUMN_IN_TABLE_C"))
@Embedded
private B b;
like image 102
Mikko Maunu Avatar answered Sep 30 '22 21:09

Mikko Maunu