如何使用休眠将连接查询列映射到 class?
how map join query columns to a class using hibernate?
我试图创建一个查询,其中 returns 来自两个表的不同列,我希望将查询列映射到用户定义 class。
我的学生模型:
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table
public class Students {
public Students() {
}
public Students(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Column
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String age;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "Student_Course",
joinColumns = @JoinColumn(name="studentID"),
inverseJoinColumns = @JoinColumn(name="courseID"))
private List<Course> courses;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
我的课程模型:
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table
public class Course {
public Course() {
}
public Course(String courseName, String unitCount) {
this.courseName = courseName;
this.unitCount = unitCount;
}
@Column
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "CourseName")
private String courseName;
@Column
private String unitCount;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "Student_Course",
joinColumns = @JoinColumn(name="courseID"),
inverseJoinColumns = @JoinColumn(name="studentID"))
private List<Students> students;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "teacherID")
private Teachers teachers;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getUnitCount() {
return unitCount;
}
public void setUnitCount(String unitCount) {
this.unitCount = unitCount;
}
public List<Students> getStudents() {
return students;
}
public void setStudents(List<Students> students) {
this.students = students;
}
public Teachers getTeachers() {
return teachers;
}
public void setTeachers(Teachers teachers) {
this.teachers = teachers;
}
}
我在服务层的查询:
@Transactional
public List<StudentInfo> getStudentInfo(){
Session session = sf.openSession();
Query hql = session.createQuery("select std.firstName, std.lastName, c.courseName from Students std join std.courses c");
var data = hql.list();
session.close();
return data;
}
我想将查询列映射到这个简单的 class :
package com.example.demo.ViewModels;
public class StudentInfo {
private String firstName;
private String lastName;
private String courseName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
最后..
我的控制器 Class :
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView Index() {
List<StudentInfo> data = studentRepository.getAll();
return new ModelAndView("indexView", "data", data);
}
notice : i`m using thymeleaf in this project.
please help me.
thanks.:D
如果您使用 Spring Data JPA
,您应该可以使用 @Query
注释在存储库中执行此操作:
@Query(value = "SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c"
List<StudentInfo> getAllStudentInfo();
确保你在 StudentInfo
中有一个全参数构造函数。
如果你使用Hibernate,几乎是一样的:
entityManager.createQuery("SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c",
StudentInfo.class)
编辑:我担心它在使用 join
时是否应该起作用,但不管怎样都试一试。
我试图创建一个查询,其中 returns 来自两个表的不同列,我希望将查询列映射到用户定义 class。
我的学生模型:
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table
public class Students {
public Students() {
}
public Students(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Column
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String age;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "Student_Course",
joinColumns = @JoinColumn(name="studentID"),
inverseJoinColumns = @JoinColumn(name="courseID"))
private List<Course> courses;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
我的课程模型:
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table
public class Course {
public Course() {
}
public Course(String courseName, String unitCount) {
this.courseName = courseName;
this.unitCount = unitCount;
}
@Column
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "CourseName")
private String courseName;
@Column
private String unitCount;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "Student_Course",
joinColumns = @JoinColumn(name="courseID"),
inverseJoinColumns = @JoinColumn(name="studentID"))
private List<Students> students;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "teacherID")
private Teachers teachers;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getUnitCount() {
return unitCount;
}
public void setUnitCount(String unitCount) {
this.unitCount = unitCount;
}
public List<Students> getStudents() {
return students;
}
public void setStudents(List<Students> students) {
this.students = students;
}
public Teachers getTeachers() {
return teachers;
}
public void setTeachers(Teachers teachers) {
this.teachers = teachers;
}
}
我在服务层的查询:
@Transactional
public List<StudentInfo> getStudentInfo(){
Session session = sf.openSession();
Query hql = session.createQuery("select std.firstName, std.lastName, c.courseName from Students std join std.courses c");
var data = hql.list();
session.close();
return data;
}
我想将查询列映射到这个简单的 class :
package com.example.demo.ViewModels;
public class StudentInfo {
private String firstName;
private String lastName;
private String courseName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
最后..
我的控制器 Class :
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView Index() {
List<StudentInfo> data = studentRepository.getAll();
return new ModelAndView("indexView", "data", data);
}
notice : i`m using thymeleaf in this project.
please help me.
thanks.:D
如果您使用 Spring Data JPA
,您应该可以使用 @Query
注释在存储库中执行此操作:
@Query(value = "SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c"
List<StudentInfo> getAllStudentInfo();
确保你在 StudentInfo
中有一个全参数构造函数。
如果你使用Hibernate,几乎是一样的:
entityManager.createQuery("SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c",
StudentInfo.class)
编辑:我担心它在使用 join
时是否应该起作用,但不管怎样都试一试。