简体   繁体   English

Spring-Hibernate,多对多关系查询

[英]Spring-Hibernate, Many to Many relationship query

I have two bean classes Student and Course which has many to many relationship with each other. 我有两个bean类Student和Course,它们之间有很多关系。 For eg. 例如。 one student can register for multiple courses and vice versa. 一个学生可以注册多个课程,反之亦然。 I have used HibernateTemplate to save objects into Oracle DB. 我已经使用HibernateTemplate将对象保存到Oracle DB中。 Following are Student, Course and StudentDao classes. 以下是学生,课程和StudentDao课程。

Student Class 学生班

package com.springhibernate;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="Student")

public class Student {
    private int studentId;
    private String firstName;
    private String lastName;
    private Set<Course> courses;

@Id
@Column(name="student_id")
public int getStudentId() {
    return studentId;
}
public void setStudentId(int studentId) {
    this.studentId = studentId;
}

@Column(name="first_name")
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Column(name="last_name")
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Student_Course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
public Set<Course> getCourses() {
    return courses;
}
public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

}

StudentDao class 学生道课

package com.springhibernate;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class StudentDao {

private static Helper helper = new Helper();

HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
    this.template = template;
}

// method to save student
public void saveStudent(Student s) {
    template.save(s);
}

// method to return one employee of given id
public Student getById(int id) {
    Student s = (Student) template.get(Student.class, id);
    return s;
}

public List<Course> findCourse(){
    List<Course> list = template.find("from Course");
    return list;
}
}

Course Class 课程课程

package com.springhibernate;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name="Course")
public class Course {

private int courseId;
private String courseName;
private Set<Student> students;

@Id
@Column(name="course_id")
public int getCourseId() {
    return courseId;
}
public void setCourseId(int courseId) {
    this.courseId = courseId;
}

@Column(name="course_name")
public String getCourseName() {
    return courseName;
}
public void setCourseName(String courseName) {
    this.courseName = courseName;
}

@ManyToMany(cascade=CascadeType.ALL,mappedBy="courses")
public Set<Student> getStudents() {
    return students;
}
public void setStudents(Set<Student> students) {
    this.students = students;
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    return this.courseId;
}

@Override
public boolean equals(Object o) {
    // TODO Auto-generated method stub
    Course temp = (Course) o;
    return (this.courseId==temp.courseId);

}
}

I have following two queries 我有以下两个查询

  1. I am able to save data in student_course table successfully. 我能够成功将数据保存在student_course表中。 I was wondering if I want to retrieve data from student_course table, how can I do it using HibernateTemplate or is there any other way to do so? 我想知道我是否要从student_course表中检索数据,如何使用HibernateTemplate进行处理,或者还有其他方法可以这样做? For example, query is like 例如,查询就像

     select course_id from student_course where student_id=1 

    Please note I want just the course id column not complete row. 请注意,我只希望课程ID列不完整。

  2. If in student_course table I want one more column say course_name (from course table), how can I do that? 如果我要在student_course表中再增加一列,例如course_name(来自课程表),该怎么办?

You can do it in the following way, but keep in mind that the HibernateTemplate is an old API and you should use for example the EntityManager(Factory). 您可以通过以下方式进行操作,但是请记住,HibernateTemplate是一个旧的API,您应该使用EntityManager(Factory)。 You can get it via the Peristence class. 您可以通过Peristence类获得它。

hibernateTemplate.execute(new HibernateCallback<List>() {

    public String doInHibernate(Session s)
            throws HibernateException, SQLException {
        SQLQuery sql=s.createSQLQuery("select course_id from student_course where student_id=?");
            sql.setParameter(0, adventureId);
        sql.addScalar(studentID);
        return sql.list();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM