Pradyut Bhattacharya
Pradyut Bhattacharya

Reputation: 5748

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

Upvotes: 1

Views: 14543

Answers (3)

Pascal Thivent
Pascal Thivent

Reputation: 570345

Your "normal" class is very likely not a managed component i.e. a class whose life cycle is managed by the container (like Servlets, Servlet Filters, JSP tag handlers, JSF Managed Beans, ...) and can't benefit from resource injection1. So neither the UserTransaction nor the EntityManagerFactory are injected here, hence the NullPointerException.

Honestly, you should try to use a container managed EntityManager, this would make your code less messy. If you cannot get it injected, get it via a JNDI lookup. See the resource below.

1 Have a look at Web Tier to Go With Java EE 5: A Look at Resource Injection for a nice overview of what can be injected, and where.

Resources

References

  • JPA 1.0 specification
    • Section 5.2 "Obtaining an Entity Manager"
    • Section 5.6 "Container-managed Persistence Contexts"

Upvotes: 0

Pradyut Bhattacharya
Pradyut Bhattacharya

Reputation: 5748

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

Upvotes: 1

Bozho
Bozho

Reputation: 597096

  1. Perhaps your bean isn't managed - i.e. it's not part of any context (spring, EJB). How are you creating your object?
  2. You really should not call createEntityManager() - inject one using @PersistenceContext
  3. You must be absolutely sure you need JTA before using it.
  4. You seem to be using PersistenceUnit, but then re-assign the etm - I'd suggest drop both and see p2 above.

If you are not using any dependecy injection at all, then drop all the annotations, retain the current code, and type:

em.getTransaction().begin();
...
em.getTransaction().commit();

(and define RESOURCE_LOCAL in your persistence.xml. You really don't need JTA)

Upvotes: 1

Related Questions