Search code examples
javaormjpajta

update using JPA


I m using glassfish v2 and persistence in a web application.

I m calling persistence codes using a normal java class file inside a web Application

I can select easily using this code: -

   @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;


public List fname (String id) {
    String fname = null;
    List persons = null;
    //private PersistenceManagerFactory persistenceManagerFactory;

    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        em = emf.createEntityManager();
        persons = em.createQuery("select r from Roleuser r").getResultList();

        int i=0;
        for (i=0;i<persons.size(); i++)
            System.out.println("Testing n "+ i +" " + persons.get(i));

    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

I want to update using JTA as the persistence.xml file has transaction-type="JTA"

When i try using update using this code i get a nullPointerException without any traces in the log

     @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;
Context context;
@Resource
private UserTransaction utx;

public List fname (String id) {

    String fname = null;
    List persons = null;


    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        utx.begin();
        em = emf.createEntityManager();

        int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh I' where r.userID=9").executeUpdate();

        utx.commit();


    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

Any help

Thanks

Pradyut


Solution

  • well the code should be without any nightmares...(atleast for me in glassfish)
    with the persistence.xml having

    <persistence-unit name="WebApplicationSecurityPU" transaction-type="RESOURCE_LOCAL">
    

    the code

    @PersistenceUnit
    public EntityManagerFactory emf;
    public EntityManager em;
    
    
    
    
    public EntityManager getEm() {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");
        em = emf.createEntityManager();
        return em;
    }
    
    public List fname (String id) {
    
        String fname = null;
        List persons = null;
    
    
        try {
            System.out.println("test");
    
            em = this.getEm();
    
    
            em.getTransaction().begin();
            int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh H' where r.userID=9").executeUpdate();
    
            em.getTransaction().commit();
    
    
        } catch(Exception e) {
            System.out.println("" + e);
        }
        finally {
            if(em != null) {
                em.close();
            }
        }
        return persons;
    }
    

    Any improvements are welcome...(actually needed...) (How to go about using @PersistenceContext)

    Thanks

    Pradyut