在@Embeddable类中使用@Embedded

San*_*Rao 6 java spring hibernate jpa java-ee

我可以在hibernate中的@Embeddable类中使用@Embedded吗?

示例:A是diffirent类中的元素集合.

@Embeddable
class A {

    @Embedded
    B b;
}

@Embeddable
class B {

    @Embedded
    C c;
}


@Embeddable
class C {

    @Embedded
    D D;
}

@Embeddable
class D {



}
Run Code Online (Sandbox Code Playgroud)

这种类似的东西在休眠中是否有效?第三级嵌套.

Joh*_*sen 5

是的,在Hibernate中嵌套@Embedded对象是有效的。

直接从文档(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e714):

@Entity
public class Person {

    @Embedded
    Address homeAddress;
}          

@Embeddable
public class Address {

    @Embedded
    Country nationality;
}            

@Embeddable
public class Country {
    ...
}    
Run Code Online (Sandbox Code Playgroud)

(删除了多余的代码以突出显示嵌套@Embedded)