简体   繁体   English

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

[英]AttributeOverride set null value to unmapped field

I have class 我有课

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

// getters, setters
}

and I have mapped class 我已经绘制了类

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

}

I want that 'filed3' in myClassField2 be 'null', becouse 'field3' hasn't DB mapping. 我希望myClassField2中的'filed3'为'null',因为'field3'没有DB映射。 What can I do? 我能做什么? JPA search value for 'field3' in column 'field3' 'field3'列中'field3'的JPA搜索值

In Hibernate all passed normal! 在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/>  

But I need this in JPA 但我在JPA中需要这个

What you want to do is impossible. 你想做什么是不可能的。 EIther a field is persistent, or it's transient. 一个领域是持久的,或者是暂时的。 It can't be sometimes one and sometimes the other. 它有时不是一个,有时是另一个。

Consider defining a MyClass1 with just two fields, and a MyClass2 extending MyClass1 and adding the third field. 考虑使用两个字段定义MyClass1,并扩展MyClass2以扩展MyClass1并添加第三个字段。 Use the first one for myClassField2 , and the second one for myClassField1 . 将第一个用于myClassField2 ,将第二个用于myClassField1

Note that, in your example, you have mapped column1 and column2 twice, which is also incorrect. 请注意,在您的示例中,您已将column1和column2映射两次,这也是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM