I’m trying to use composition in hibernate with annotations.
I have:
@Entity
@Table(name = "Foo")
public class Foo {
private Bar bar;
public void setBar(Bar bar){...}
public Bar getBar() {...)
}
public class Bar {
private double x;
public void setX(double x) {...}
public double getX() {...}
}
And when trying to save Foo, I’m getting
Could not determine type for entity
org.bla.Bar at table Foo for columns:
[org.hibernate.mapping.Column(bar)]
I tried putting an @Entity annotation on Bar, but this gets me:
No identifier specified for entity
org.bla.Bar
The mechanism is described in this section of the reference docs:
5.1.5. Embedded objects (aka components)
Apparently hibernate uses JPA annotations for this purpose, so the solution referred to by Ralph is correct
In a nutshell:
if you mark a class
Addressas@Embeddableand add a property of typeAddressto classUser, marking the property as@Embedded, then the resulting database tableUserwill have all fields specified byAddress.See Ralph’s answer for the code.