12

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.

asked Mar 1, 2013 at 23:05

2 Answers 2

8

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;
}
Marc Bannout
4511 gold badge6 silver badges17 bronze badges
answered Mar 1, 2013 at 23:55
Sign up to request clarification or add additional context in comments.

3 Comments

I'm able to get upto the second level of nesting is third level of nesting valid. My issue is i'm unable to rename the thrid level parameters with attributeoveride of columns. Thanks for replying !
Hmm, are your column names duplicated?
No, They are different in class D
5

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;
answered Oct 1, 2014 at 10:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.