我有这个应用程序,我使用 JSF 2.0 和 EclipseLink,我为 MySQL 创建的数据库创建了实体,使用 netbeans 7.1.2 创建了这些实体,它是自动创建的。
然后我使用会话 bean 来处理这些实体,问题是em.createQuery
总是返回 null,尽管我在实体中检查了 NamedQueries 并且它们完全匹配
来自名为查询的实体的样本:-
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
注意我如何在会话 bean 中使用这个 findByEmail 查询:-
public Users findByEmail(String email){
em.getTransaction().begin();
String find = "Users.findByEmail";
Query query = em.createNamedQuery(find);
query.setParameter("email", email);
Users user = (Users) query.getSingleResult();
但它总是从这个 em.createNamedQuery 返回 null,我首先尝试使用 .createQuery,但它也不好。
异常的堆栈跟踪
Caused by: java.lang.NullPointerException
at com.readme.entities.sessionBeans.UsersFacade.findByEmail(UsersFacade.java:48)
at com.readme.user.signup.SignupBean.checkAvailability(SignupBean.java:137)
at com.readme.user.signup.SignupBean.save(SignupBean.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
这里的问题似乎是什么?