Home JPA Entity fields in separate bean using property access
Reply: 0

JPA Entity fields in separate bean using property access

Bertil Alexandersson
1#
Bertil Alexandersson Published in 2017-12-05 12:58:33Z

I am currently trying to find a solution to the following problem. In a customer project, they are using JPA Entities on the backend and for the frontend, they use classical DTOs. When transferring data from backend to frontend, the DTOs are created and on the frontend, they can be manipulated and sent back to the backend where the data is stored.

Now, the crux of the matter is that the DTOs have custom types which include some additional logic so the two classes do not have the same method definitions. Which makes it more difficult to transfer the data back and forth. In order to simplify the transition of data from one to the other (either DTO -> Entity or Entity -> DTO), I was thinking if the following setup could work.

For JPA Entities, is it possible to contain the values (fields) in a separate class and use property access in order to get and set these values? See the following code example:

@Entity
public class MyEntity extends MyAbstractSuperClass {

    private MyValueContainer valueContainer = new MyValueContainer();

    @Transient
    public MyValueContainer getValueContainer() {
        return this.valueContainer;
    }

    public void setValueContainer(MyValueContainer valueContainer) {
        this.valueContainer = valueContainer;
    }

    @Column(name="myField")
    public java.math.BigDecimal getMyField(){
        return this.valueContainer.getMyField() ;
    }

    public void setMyField( java.math.BigDecimal myField ){
        this.valueContainer.setMyField(myField);
    }
}

public class MyValueContainer implements Serializable {

    public MyValueContainer() {}

    private java.math.BigDecimal myField;

    public java.math.BigDecimal getMyField(){
        return this.myField ;
    }

    public void setMyField( java.math.BigDecimal myField ){
        this.myField = myField ;
    }
}

public class MyDto implements Serializable {

    private MyValueContainer valueContainer = new MyValueContainer();

    public MyValueContainer getValueContainer() {
        return this.valueContainer;
    }

    public void setValueContainer(MyValueContainer valueContainer) {
        this.valueContainer = valueContainer;
    }

    public com.dummy.MySpecialType getMyField(){
        com.dummy.MySpecialType mst = new com.dummy.MySpecialType();
        mst.setValue(this.valueContainer.getMyField());
        return mst;
    }

    public void setMyField( com.dummy.MySpecialType myField ){
        this.valueContainer.setMyField(myField.getValue());
    }
}

This would make it easy to switch the values from one object instance to the other, like a cartridge. However, I have not found anyone else doing this so I really doubt this is possible. On the other hand, I haven't found the place in JPA spec which prohibits this. Anyone?

EDIT: I may have found the place in the JPA spec which prohibits this:

The persistent state of an entity is represented by instance variables, which may correspond to Java-Beans properties. An instance variable must be directly accessed only from within the methods of the entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity’s methods—i.e., accessor methods (getter/setter methods) or other business methods.

Would still like to hear if I am wrong on this.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321424 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO