AttributeOverride将null值设置为未映射的字段

时间:2012-03-09 13:37:01

标签: java jpa

我有课

@Embeddable
public class MyClass implements Serializable
{
   private String field1;
   private String field2;
   private String field3;

// getters, setters
}

我已经映射了类

public class MappedClass implements Serializable
{
   @Embedded
   @AttributeOverrides(
      {
        @AttributeOverride(name = "field1", column = @Column(name = "column1")),
        @AttributeOverride(name = "field2", column = @Column(name = "column2")),
        @AttributeOverride(name = "field3", column = @Column(name = "column3"))
      })
   private MyClass myClassField1;

   @Embedded
   @AttributeOverrides(
      {
        @AttributeOverride(name = "field1", column = @Column(name = "column1")),
        @AttributeOverride(name = "field2", column = @Column(name = "column2")),
      })
   private MyClass myClassField2;

// setters, getters

}

我希望myClassField2中的'filed3'为'null',因为'field3'没有DB映射。 我能做什么? “field3”列中“field3”的JPA搜索值

在Hibernate中全部通过正常!

   <component name="myClassField1" class="MappedClass">
           <property name="field1" type="string" column="column1" />
           <property name="field2" type="string" column="column2" />
           <property name="field3" type="string" column="column3" />
   <component/>  

   <component name="myClassField2" class="MappedClass">
           <property name="field1" type="string" column="column1" />
           <property name="field2" type="string" column="column2" />
   <component/>  

但我在JPA中需要这个

1 个答案:

答案 0 :(得分:4)

你想做什么是不可能的。一个领域是持久的,或者是暂时的。它有时不是一个,有时是另一个。

考虑使用两个字段定义MyClass1,并扩展MyClass2以扩展MyClass1并添加第三个字段。将第一个用于myClassField2,将第二个用于myClassField1

请注意,在您的示例中,您已将column1和column2映射两次,这也是错误的。