tags:

views:

302

answers:

3
+1  Q: 

update using JPA

Hi 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

A: 
  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)

Bozho
well that did that...i just want to point out that without the annotation@PersistenceUnitit will give a oracle.toplink.essentials.exceptions.DatabaseExceptionanyway i just want to know what's the relation of JTA with EJB(or spring) and why cant be used with normal class files...any referencesthanks
Pradyut Bhattacharya
There are ways, but you don't need them. And they are too complex. Stick to this for now. You can always google for JTA and read, but you'll have to read a lot ;)
Bozho
omg!!!... i could not reaaly get wats happening but without these the code would not run@PersistenceUnit public EntityManagerFactory emf; EntityManager em;@Resource
Pradyut Bhattacharya
it won't compile perhaps. I said remove the annotations only, not the variables themselves.
Bozho
ha ha ... obviously not the variables but the annotations @PersistenceUnit and @Resource are required.... without the @Resource it gives error...Internal Exception: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specifiedError Code: 0now where does odbc and microsoft comes in.. i m using mySQL...omG!!!
Pradyut Bhattacharya
you have something terribly wrong in your application and you must sit down, read a lot and dig a lot in order to clean it up.
Bozho
i searched every other code googling down and in other books... but to my weirdest imagination i could not possibly find any code to call persistence using a normal class file in a web application....any reference would be highly regarded....thanks...
Pradyut Bhattacharya
well i have one more question...http://stackoverflow.com/questions/3567438/select-from-two-tables-using-jpql
Pradyut Bhattacharya
+1  A: 

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

Pradyut Bhattacharya
A: 

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"
Pascal Thivent
well i have one more question...http://stackoverflow.com/questions/3567438/select-from-two-tables-using-jpql
Pradyut Bhattacharya