user2866264
user2866264

Reputation: 3439

How to implement generic business logic methods from JPA database entity class?

I want suggestion regarding a scenario I've been thinking of doing if possible. Suppose I have some JPA database entity class like:

@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;
    }
}

I am using EJB services. I can use separate business logic methods to make CRUD operation over these entities. Is it possible to use a generic template CRUD operations for these entity classes? Like if I want to create new person I will provide the Person entity class and fields to set as parameter and my generic method will create a new Person record and will do the same job for Read, Update and Delete operation as well.

Any respective example will be highly appreciated.

Thank You

Upvotes: 0

Views: 2204

Answers (2)

Alan Hay
Alan Hay

Reputation: 23246

If you are using Spring then use Spring Data which will do all this for you.

http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/repositories.html#repositories.core-concepts

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42040

Using EJB and JPA

You can consider an abstract class for the service layer:

public abstract class AbstractFacade<E extends Serializable, 
                                     PK extends Serializable> {

    private final transient Class<E> entityClass;

    public AbstractFacade(final Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(final E entity) {
        final EntityManager entityManager = getEntityManager();
        entityManager.persist(entity);
    }

    public final E find(final PK id) {
        return getEntityManager().find(entityClass, id);
    }

    // Other common operations

}

And a particular service:

@Stateless
public class PersonFacade extends AbstractFacade<Person, Long> {

    @PersistenceContext(unitName = "MyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PersonFacade() {
        super(Person.class);
    }

    // Other methods of this service

}

Using Spring and Hibernate

You could have a abstract base class for common DAO methods.

public abstract class AbstractDAO<E extends Serializable, 
                                 PK extends Serializable> {

    private final transient Class<E> entityClass;

    public AbstractDAO(final Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public final E find(final PK id) {
        return getEntityManager().find(entityClass, id);
    }

    // Another common methods

}

In every DAO implementation, you can put particular methods for that DAO.

@Repository
public final class PersonDAO extends AbstractDAO<Person, Long> {

    @Autowired
    private transient EntityManagerFactory emf;

    public PersonDAO() {
        super(Person.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    // particular methods for this DAO

}

What about if the user not exists? Put this logic in the service layer.

@Service
public final class PersonService {

    private static final Logger LOG = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    private transient PersonDAO personDAO;

    public Person findPerson(final Long id) {
        return personDAO.find(id);
    }

}

Upvotes: 1

Related Questions