我是Java初学者
我想用JPQL运行查询,但是我给出了一条错误消息
错误消息
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select u from user u where u.email = :pr1 and u.password = : pr2].
[27, 64] The expression is not a valid conditional expression.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1583)
... 85 more
查询
@EJB
private UserFacade usersfacede;
private String redirect;
public void dologin(){
String query1 ="select u from user u where u.email = :pr1 and u.password = : pr2";
List<User> loginuser = usersfacede.araStr(query1, email.trim(), password.trim(), "", "", "");
if(loginuser.size()>0){
Util.setSessionAttribute("name", loginuser.get(0).getName());
redirect="index.xhtml?faces-redirect=true";
}else{
redirect="";
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("error", "") );
}
}
使UserFacede扩展AbstractFacade
AbstractFacade.java中的arastr函数
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.entity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
/**
*
* @author seref
*/
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
public List<T> araStr(String sorgu, String pr1, String pr2, String pr3, String pr4, String pr5) {
Query query = getEntityManager().createQuery(sorgu);
if (pr1 != null & !"".equals(pr1)) {
query.setParameter("pr1", pr1);
}
if (pr2 != null & !"".equals(pr2)) {
query.setParameter("pr2", pr2);
}
if (pr3 != null & !"".equals(pr3)) {
query.setParameter("pr3", pr3);
}
if (pr4 != null & !"".equals(pr4)) {
query.setParameter("pr4", pr4);
}
if (pr5 != null & !"".equals(pr5)) {
query.setParameter("pr5", pr5);
}
System.out.print(query.toString());
List<T> list = query.getResultList();
return (List<T>) list;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
有人知道哪里出错了吗?
答案 0 :(得分:1)
从
更改您的查询select u from user u where u.email = :pr1 and u.password = : pr2
到
select u from user u where u.email = :pr1 and u.password = :pr2
即。删除:和pr2之间的空格。