【问题标题】:Using @Embedded in @Embeddable class在@Embeddable 类中使用@Embedded
【发布时间】:2013-02-16 13:55:37
【问题描述】:

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

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

@Embeddable
class A {

    @Embedded
    B b;
}

@Embeddable
class B {

    @Embedded
    C c;
}


@Embeddable
class C {

    @Embedded
    D D;
}

@Embeddable
class D {



}

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

【问题讨论】:

    标签: java spring hibernate jakarta-ee jpa


    【解决方案1】:

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

    @Embedded
    @AttributeOverride(name = "c.D.foo", column = @Column(name = "bar"))
    B b;
    

    【讨论】:

      【解决方案2】:

      是的,在 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)

      【讨论】:

      • 我能够达到第二级嵌套是第三级嵌套有效。我的问题是我无法使用列的属性覆盖重命名第三级参数。谢谢回复!
      • 嗯,你的列名重复了吗?
      • 不,它们在 D 类中是不同的
      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多