嗨,这是我的实体StudentEntity.java,
@Entity
@NamedQueries({
@NamedQuery(name="totalStudentSQLQuery",
query="select count(*) as MAX_STUDENT from tblstudent"),
@NamedQuery(name="queryForPagination",
query="SELECT s.studentid AS studentid, s.studentname AS studentname, s.contactno AS contactno, s.deptid AS deptid "+
"FROM (SELECT Row_number() OVER() AS row_num, studentid, studentname, contactno, deptid FROM tblstudent order by studentId asc) s" +
" WHERE s.row_num >=:minRow AND s.row_num <=:maxRow")
})
@Table(name="TBLSTUDENT")
public class StudentEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 100034222342L;
@Id
@Column(name="STUDENTID")
private Integer studentId;
@Column(name="STUDENTNAME")
private String studentName;
@Column(name="CONTACTNO")
private String contactNumber;
.....
.....
}
我正在使用Hibernate 4.0 jar用于持久性提供程序,并使用JPA,
我的persistense.xml如下,
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="forPractise" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/app</jta-data-source>
<class>com.entity.StudentEntity</class>
<class>com.entity.DeptEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
现在,当我创建此项目的war文件并尝试在Glassfish服务器中部署时,我收到错误 意外的标记: ( org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:(第1行,第116列附近[SELECT s.studentid AS studentid,s.studentname AS studentname,s.contactno AS contactno,s.deptid AS deptid FROM(SELECT Row_number()OVER()AS row_num,studentid,studentname,contactno,deptid FROM tblstudent order by studentId asc)s WHERE s.row_num&gt; =:minRow AND s.row_num&lt; =:maxRow]
任何人都可以告诉我,我在NamedQuery中犯了错误,
我正在使用Derby数据库。
答案 0 :(得分:3)
看起来您正在使用标准SQL查询。对于NamedQueries,您必须使用JPA-QL定义查询。
答案 1 :(得分:0)
如果您想使用经典的SQL数据库,您必须使用注释查询 @NamedNativeQuery。