如何解决Spring mvc和休眠中意外的令牌异常?



我的应用程序是基于Spring MVC和休眠的应用程序。在下面的 hql 语句中,我将结果集映射到名为"Result"的自定义类。这是我的HQL声明,

String hql=
"select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name)"+
" from com.gavs.beans.College c" + 
" join com.gavs.beans.Department d on d.institutes.college_id = c.college_id";
org.hibernate.query.Query q = session.createQuery(hql);
List<Result> r1= q.list();

return r1;

我在 hql 语句中得到以下这些异常

Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:34: unexpected token: distinct
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:34: unexpected token: distinct
line 1:34: unexpected token: distinct
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:43: unexpected token: d
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:43: unexpected token: d
line 1:43: unexpected token: d
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:56: unexpected token: as
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:56: unexpected token: as
line 1:56: unexpected token: as
.
.
.
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:92: expecting EOF, found ')'
Jun 09, 2020 3:33:24 PM org.hibernate.hql.internal.ast.ErrorTracker reportError
ERROR: line 1:92: expecting EOF, found ')'
line 1:92: expecting EOF, found ')'
.
.
.
Jun 09, 2020 3:33:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [studentDetails] in context with path [/HibernateWithQuery] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 34 [select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name) from com.gavs.beans.College c join com.gavs.beans.Department d on d.institutes.college_id = c.college_id]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 34 [select new com.gavs.beans.Result(distinct(d.dept_name) as dept_name,distinct(c.college_name) as college_name) from com.gavs.beans.College c join com.gavs.beans.Department d on d.institutes.college_id = c.college_id]

这是学生实体(学生.java(

package com.gavs.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT_TABLE")
public class Student{
@Id
@Column(name="Roll_No")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int roll_no;
@Column(name="Name")
private String name;
@Column(name="Age")
private int age;
@Column(name="Address")
private String address;
@ManyToOne
@JoinColumn(name="Dept_Id")
private Department departments;

public Student() {
super();
}
public int getRoll_no() {
return roll_no;
}
public void setRoll_no(int roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

public Department getDepartments() {
return departments;
}
public void setDepartments(Department departments) {
this.departments = departments;
}

}

这是部门实体(部门.java(

package com.gavs.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="DEPARTMENT_TABLE")
public class Department {
@Id
@Column(name="Dept_Id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int dept_id;
@Column(name="Dept_Name")
private String dept_name;
@Column(name="Dept_Code")
private int dept_code;
@ManyToOne
@JoinColumn(name="College_Id")
private College institutes;

public int getDept_id() {
return dept_id;
}

public Department() {
super();
}

public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
public String getDept_name() {
return dept_name;
}
public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}
public int getDept_code() {
return dept_code;
}
public void setDept_code(int dept_code) {
this.dept_code = dept_code;
}

public College getColleges() {
return institutes;
}

public void setColleges(College institutes) {
this.institutes = institutes;
}

}

这是学院实体(学院.java(

package com.gavs.beans;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="COLLEGE_TABLE")
public class College {
@Id
@Column(name="College_Id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int college_id;
@Column(name="College_Name")
private String college_name;
@Column(name="College_Address")
private String college_address;
@OneToMany(mappedBy="institutes",cascade=CascadeType.ALL)
private List<Department> departments;
public College() {
}
public int getCollege_id() {
return college_id;
}
public void setCollege_id(int college_id) {
this.college_id = college_id;
}
public String getCollege_name() {
return college_name;
}
public void setCollege_name(String college_name) {
this.college_name = college_name;
}
public String getCollege_address() {
return college_address;
}
public void setCollege_address(String college_address) {
this.college_address = college_address;
}
public List<Department> getDepartments() {
return departments;
}
public void setDepartments(List<Department> departments) {
this.departments = departments;
}
}

尝试将接口投影与 JPA 存储库一起使用

public interface Result{
String getDepartmentName();
String getCollegeName();
}

在您的 Jpa 存储库中:

@Query("select distinct d.dept_name as departmentName, d.institutes.college_name as collegeName from Department d")
List<Result> findDistinctDepartmentAndCollegeName();

最新更新