Question

I created in my EJB like a wrapper for entities. In this wrapper I have my business-logic and my validations commons for this entity. I want to know how send my wrapper to CRUD (but I don´t want that my wrapper to be@Entity).

Any idea???

Was it helpful?

Solution

It is best not to use your EJB as a wrapper but some other java class with the specific purpose to wrap an entity.

The wrapper class should wrap an entity, thus it should be a one to one association (not to be confused with jpa, i mean java association / composition). So basically you follow the decorator pattern.

Then you create all methods that you need, really helpful to have the same methods as the entity you are wrapping. Then within the method bodies call the corresponding method of the entity instance you are wrapping.

Example,

@Entity
public class Person {

    @Column(name = "ID")
    private Long id;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;

    public String getFirstName(){
      return this.firstName;
    }

    public void setFirstName(String firstName){
      this.firstName = firstName;
    }

    public String getLastName(){
      return this.lastName;
    }

    public void setLastName(String lastName){
     this.lastName = lastName;
    }
}

public class PersonWrapper{
  private Person person;
  private String someCalculatedField;
  public PersonWrapper(Person person){
    this.person = person;
  }

  public Person getPerson(){
    return this.person;
  }

  public void setPerson(Person person){
    this.person = person;
  }

  public String getFirstName(){
    /* possibly do transformations */
    return person.getFirstName();
  }

  public void setFirstName(String firstName){
    /* possibly do validations, transformations*/
    person.setFirstName(firstName);
  }

  public String getSomeCalculatedField(){
  /*do stuff with person or other entities as well and populate the calculated field,
    then return it
  */
    return this.someCalculatedField;
  }


}

Maybe it is not clear but after you are finished with the CRUD functions or any other manipulation of the wrapper instance, in order to persist your changes via jpa you need to retrieve the jpa entity (i.e. person in your case from wrapper's method getPerson() ) and call persist , merge , remove or whatever to that instance.

EDIT

Based on the comments i would like to make clear that making the wrapper inherit from the entity would only benefit to decouple the non-jpa (e.g. CRUD UI ) code from the wrapper itself, for example by calling Person person = new PersonWrapper(personEntity); instead of PersonWrapper person = new PersonWrapper(personEntity);.

Even after inheriting it is mandatory to override the methods in the wrapper since the wrapper's method bodies call code on the wrapped instance not on the wrapper itself. Not overriding the methods will call the inherited methods on the wrapper itself. The latter is a mistake for two reasons,

1.data is stored/retrieved from the wrong instance (wrapper not the wrapped entity)

2.data should not be stored in the wrapper since it cannot be used in jpa functions as it is not an entity (no jpa inheritance there), only the wrapped instance can be used. Also the wrapper did not come from the managed jpa context so it has no data related to persisence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top