我使用netbeans向导创建了实体bean,并尝试从数据库中获取数据。无论我使用什么SQL查询,它都不起作用。我尝试使用由向导创建的命名查询:
@NamedQuery(name = "Usr.findAll", query = "SELECT u FROM Usr u")
它返回:
Caused by: Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [Usr.findAll], line 1, column 0: unexpected token [Usr].
如果我尝试;
SELECT uid FROM usr;
我明白了:
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT uid FROM usr;], line 0, column -1: unexpected end of query.
Internal Exception: MismatchedTokenException(-1!=78)
即使我尝试:
SELECT * FROM usr
我明白了:
Caused by: Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query [SELECT * FROM usr], line 1, column 7: unexpected token [*].
我获取数据的路线是:
@PersistenceContext
EntityManager em;
....
em=Persistence.createEntityManagerFactory("SchoolPU").createEntityManager();
List users = em.createQuery("SELECT * FROM usr").getResultList();
任何人都可以帮我解决这个微不足道的问题吗?
Usr实体类:
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author danizmax
*/
@Entity
@Table(name = "USR")
@NamedQueries({
@NamedQuery(name = "Usr.findAll", query = "SELECT u FROM Usr u"),
@NamedQuery(name = "Usr.findByUid", query = "SELECT u FROM Usr u WHERE u.uid = :uid"),
@NamedQuery(name = "Usr.findByPassword", query = "SELECT u FROM Usr u WHERE u.password = :password"),
@NamedQuery(name = "Usr.findByFistname", query = "SELECT u FROM Usr u WHERE u.fistname = :fistname"),
@NamedQuery(name = "Usr.findByLastname", query = "SELECT u FROM Usr u WHERE u.lastname = :lastname"),
@NamedQuery(name = "Usr.findByAddress1", query = "SELECT u FROM Usr u WHERE u.address1 = :address1"),
@NamedQuery(name = "Usr.findByAddress2", query = "SELECT u FROM Usr u WHERE u.address2 = :address2"),
@NamedQuery(name = "Usr.findByPostcode", query = "SELECT u FROM Usr u WHERE u.postcode = :postcode"),
@NamedQuery(name = "Usr.findByEmail", query = "SELECT u FROM Usr u WHERE u.email = :email"),
@NamedQuery(name = "Usr.findByPhone", query = "SELECT u FROM Usr u WHERE u.phone = :phone")})
public class Usr implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "UID", nullable = false, length = 8)
private String uid;
@Basic(optional = false)
@Column(name = "PASSWORD", nullable = false, length = 20)
private String password;
@Basic(optional = false)
@Column(name = "FISTNAME", nullable = false, length = 30)
private String fistname;
@Basic(optional = false)
@Column(name = "LASTNAME", nullable = false, length = 60)
private String lastname;
@Basic(optional = false)
@Column(name = "ADDRESS1", nullable = false, length = 100)
private String address1;
@Column(name = "ADDRESS2", length = 100)
private String address2;
@Basic(optional = false)
@Lob
@Column(name = "CITY", nullable = false)
private byte[] city;
@Basic(optional = false)
@Column(name = "POSTCODE", nullable = false, length = 10)
private String postcode;
@Column(name = "EMAIL", length = 50)
private String email;
@Column(name = "PHONE")
private Integer phone;
public Usr() {
}
public Usr(String uid) {
this.uid = uid;
}
public Usr(String uid, String password, String fistname, String lastname, String address1, byte[] city, String postcode) {
this.uid = uid;
this.password = password;
this.fistname = fistname;
this.lastname = lastname;
this.address1 = address1;
this.city = city;
this.postcode = postcode;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFistname() {
return fistname;
}
public void setFistname(String fistname) {
this.fistname = fistname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public byte[] getCity() {
return city;
}
public void setCity(byte[] city) {
this.city = city;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
@Override
public int hashCode() {
int hash = 0;
hash += (uid != null ? uid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Usr)) {
return false;
}
Usr other = (Usr) object;
if ((this.uid == null && other.uid != null) || (this.uid != null && !this.uid.equals(other.uid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "org.danizmax.Usr[uid=" + uid + "]";
}
}
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="SchoolPU" transaction-type="JTA">
<jta-data-source>jdbc/school</jta-data-source>
<properties>
</properties>
</persistence-unit>
</persistence>
我使用实体的类:
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
/**
*
* @author danizmax
*/
@Stateless
public class ValidatorBean {
@PersistenceContext
EntityManager em;
public ValidatorBean() {
}
public boolean validate(String user, String pass) {
List users = em.createQuery("SELECT * FROM usr").getResultList();
Iterator it = users.iterator();
//ignore the stupid validation it's only to try out JPA
while(it.hasNext()){
Usr u = (Usr) it.next();
if(u.getUid().equals(user) && u.getPassword().equals(pass)){
return true;
}
}
return false;
}
}
更新:为了对那些回答并奖励您的努力的人们公平,既然我实际上已经学会了技术并在现实世界中使用它,我决定通过给出最佳答案来结束这个答案这是我很久以前找到自己的最可能的解决方案。
答案 0 :(得分:5)
我遇到了同样的问题。在我的情况下,我使用方法
em.createQuery( “Usr.findAll”);
而不是
em.createNamedQuery( “Usr.findAll”);
答案 1 :(得分:4)
只有您的第一个查询是正确的,其他查询不是JPQL,因此应该导致错误。使用@NamedNativeQuery for SQL。
对于第一个查询,这似乎不是您实际用于获取错误的内容,
异常说明:语法错误解析查询[Usr.findAll],第1行,第0列:意外令牌[Usr]。
注意错误说“第0列”是“Usr”,似乎您将名称放在查询中而不是名称中。我的猜测是你在做,
em.createQuery( “Usr.findAll”)getResultList();
但应该这样做,
em.createNamedQuery( “Usr.findAll”)getResultList();
或者,
em.createQuery(“从Usr u中选择你”)。getResultList();
请参阅, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL
答案 2 :(得分:2)
您尝试获取的实体的类名是什么? (也许你需要显示放置@NamedQuery的类。)
这真的是Usr
还是User
?如果最后一个为真,那么Select u from User u
肯定会有效。
此外,您不需要@PersistenceContext
和em=Persistence.createEntityManagerFactory(...)
。使用其中一个。如果您处于托管环境中,请使用注射变体。自己创建一个实体管理器会给你一个所谓的application managed entity manager
,你的代码必须为此做更多的管理才能使事情顺利进行。
答案 3 :(得分:1)
不确定这是否会对您有所帮助,因为这个问题似乎有点老了。但是,您可能希望使用.createNativeQuery(...)
代替.createQuery(...)
,如下所示:
List users = em.createNativeQuery("SELECT * FROM usr").getResultList();
答案 4 :(得分:0)
EclipseLink倾向于在相对简单的配置问题上给出非常通用或含糊的错误消息。
我猜你错过了对Usr
中persistence.xml
课程的引用。您必须添加该类,或添加一个路径引用,告诉EclipseLink要为持久性处理哪些类。这是一个带有直接类引用的示例:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="SchoolPU" transaction-type="JTA">
<jta-data-source>jdbc/school</jta-data-source>
<class>my.package.Usr</class>
</persistence-unit>
</persistence>
类引用是最直接的方法,但如果你有很多类,可能会变得很乏味。
答案 5 :(得分:0)
我遇到了同样的问题。 Strangs解决方案 - 对我而言 - 是:与persistence.xml比较时,Toplink将“Tablename”区分大小写。因此,如果您为Java类使用标准的驼峰案例名称,则必须在查询中使用相同的驼峰案例名称。我非常喜欢完全误导性的错误信息。
答案 6 :(得分:0)
我的@NamedQuery出现了这样的问题。它根本不起作用。我解决了em.createQuery("anything.anotherthing")
更改em.createNamedQuery("anything.anotherthing")
的问题,现在它正常工作。