Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] remove boiler plate code in POJO that does JPA

This is what SessionBeans normally provide.  If you can't use EJB, perhaps
you can use Spring which has similar functionality.

Otherwise, you could use some sort of command pattern.

i.e.
    public void doSomething(MyClass data) {
       MyJPAService.processTransaction(new JPAService() {
            public void process(EntityManager em) {
              // save into database, this is the real code that differs from
method to method 
            }
      });
    }


MyJPAService {
  processTransaction(JPAService service) {
        UserTransaction transaction = null;
        try {
            EntityManager em = (EntityManager) new
InitialContext().lookup("java:comp/env/persistence/myapp");
            transaction = (UserTransaction) new
InitialContext().lookup("java:comp/UserTransaction");
            transaction.begin();

            service.process(em);

            transaction.commit();
        } catch (Exception ex) {
            try {
                  if (transaction != null) transaction.rollback();
            } catch (Exception e) {
                  //logging into log file;
            }
           
            throw new RuntimeException(ex);
        } 
}



--
View this message in context: http://eclipse.1072660.n5.nabble.com/remove-boiler-plate-code-in-POJO-that-does-JPA-tp155247p155449.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.


Back to the top