tags:

views:

42

answers:

1

I'm trying to retrieve a table using persistence framework

the code i have written is in simple java class file in webapplication

the code in the java class

EntityManager em = null;
    EntityManagerFactory emf = null;

public List fname (String id) {
    String fname = null;
    List persons = null;
    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;
}

But on running this i m getting a IllegalArgumentException the full exception being

java.lang.IllegalArgumentException: An exception occured while creating a query in EntityManager

I think the entity classes are not initialized or they have not connected with the database Thats why the IllegalArgumentException

A: 

I could correct it with some changes in 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;
}

Thanks
Pradyut

Pradyut Bhattacharya
Are you answering your own question, or amending it?
skaffman