我正在尝试使用持久性框架检索表
我编写的代码是webapplication中的简单java类文件
java类中的代码
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;
}
但是在运行这个时我得到IllegalArgumentException
java.lang.IllegalArgumentException:An 创建时出现异常 在EntityManager中查询
我认为实体类没有初始化或者没有与数据库连接
这就是IllegalArgumentException
答案 0 :(得分:0)
我可以通过代码中的一些更改来纠正它
@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;
}
谢谢你 Pradyut