Skip to main content

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

Hi there, 

I am using EclipseLink 1.x that comes with Weblogic 10.3.1 (cannot upgrade yet).   All my business methods have boilerplate code like this. It is a GWT app., the following is the backend. 

I am not able to use annotation to get EntityManager because my class is POJO and injection does not work on POJO. 

How can I remove such boilerplate code?
1. Without explicitly getting EntityManager and then Transaction everytime
2. Implement an exception handler that will rollback the transaction. But how do I get the transaction object in exception hander?    The interface org.eclipse.persistence.exceptions.ExceptionHandler  does not have that.

    public void doSomething(MyClass data) {
        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();

            // save into database, this is the real code that differs from method to method

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

Thanks,
Yan





Confidentiality Notice: The information contained in this electronic transmission is confidential and may be legally privileged. It is intended only for the addressee(s) named above. If you are not an intended recipient, be aware that any disclosure, copying, distribution or use of the information contained in this transmission is prohibited and may be unlawful. If you have received this transmission in error, please notify us by telephone (513) 229-5500 or by email (postmaster@xxxxxxxxxxx). After replying, please erase it from your computer system.


Back to the top