Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeOverride set null value to unmapped field

Tags:

java

jpa

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. What can I do? JPA search value for 'field3' in column 'field3'

In Hibernate all passed normal!

   <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

like image 465
Ilya Avatar asked Dec 04 '25 11:12

Ilya


1 Answers

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. Use the first one for myClassField2, and the second one for myClassField1.

Note that, in your example, you have mapped column1 and column2 twice, which is also incorrect.

like image 99
JB Nizet Avatar answered Dec 07 '25 00:12

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!