AttributeOverride sets an unmapped field to null

I have a class

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

// getters, setters
}

      

and I displayed the 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 the "filed3" in myClassField2 to be "null" because the "field3" field does not display the DB. What can I do? JPA for value 'field3' in column 'field3'

Everything went fine in 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

+3


source to share


1 answer


What you want to do is impossible. EIf the field is permanent or temporary. He cannot be sometimes one, and sometimes another.

Consider defining MyClass1 with two fields, and MyClass2 extending MyClass1 and adding a third field. Use the first for myClassField2

and the second for myClassField1

.



Note that your example shows column 1 and column 2 twice, which is also incorrect.

+4


source







All Articles