Can I use @Embedded in @Embeddable class in hibernate.
Example : A is an element collection in a diffirent class.
@Embeddable
class A {
@Embedded
B b;
}
@Embeddable
class B {
@Embedded
C c;
}
@Embeddable
class C {
@Embedded
D D;
}
@Embeddable
class D {
}
Is something of this kind valid in hibernate ? The third level of nesting.
2 Answers 2
Yes, it is valid in Hibernate to nest @Embedded objects.
Directly from the documentation :
Embeddable types can be nested. That is, an @Embeddable class may have an attribute whose type is itself a different @Embeddable class.
For example, from the specification :
@Embeddable public class Address {
protected String street;
protected String city;
protected String state;
@Embedded protected Zipcode zipcode;
}
@Embeddable public class Zipcode {
protected String zip;
protected String plusFour;
}
3 Comments
As mentioned by johncarl, it is possible. In order to rename nested attributes you have to specify the entire chain, using '.' as separator. For example, if class D has an attribute foo, then in class A you would need to rename it as such:
@Embedded
@AttributeOverride(name = "c.D.foo", column = @Column(name = "bar"))
B b;
Comments
Explore related questions
See similar questions with these tags.