Spring-Hibernate,多对多关系查询

时间:2015-05-10 05:09:08

标签: java spring oracle hibernate

我有两个bean类Student和Course,它们之间有很多很多关系。例如。一个学生可以注册多个课程,反之亦然。我使用HibernateTemplate将对象保存到Oracle DB中。以下是学生,课程和学生课程。

学生班级

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课程

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;
}
}

课程类

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);

}
}

我有以下两个问题

  1. 我能够成功地在student_course表中保存数据。我想知道我是否想要从student_course表中检索数据,我该如何使用HibernateTemplate来实现,还是有其他方法可以做到这一点? 例如,查询就像

    select course_id from student_course where student_id=1
    

    请注意,我只希望课程内容列不完整。

  2. 如果在student_course表格中我想要一个更多的专栏说course_name(来自课程表),我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式执行此操作,但请记住,HibernateTemplate是一个旧API,您应该使用例如EntityManager(Factory)。 你可以通过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();
    }
});