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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With