9

我可以在休眠的@Embeddable 类中使用@Embedded。

示例:A 是不同类中的元素集合。

@Embeddable
class A {

    @Embedded
    B b;
}

@Embeddable
class B {

    @Embedded
    C c;
}


@Embeddable
class C {

    @Embedded
    D D;
}

@Embeddable
class D {



}

这种东西在休眠中有效吗?第三层嵌套。

4

2 回答 2

6

是的,在 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 {
    ...
}    

(删除了额外的代码以突出嵌套@Embedded)

于 2013-03-01T23:55:36.163 回答
4

正如约翰卡尔所说,这是可能的。为了重命名嵌套属性,您必须使用“。”指定整个链。作为分隔符。例如,如果类 D 具有属性foo,那么在类 A 中,您需要将其重命名为:

@Embedded
@AttributeOverride(name = "c.D.foo", column = @Column(name = "bar"))
B b;
于 2014-10-01T10:54:06.430 回答