![](/img/trans.png)
[英]Spring-Hibernate Many-to-Many : Save an entity, or create a relationship if it exists in the database
[英]Spring-Hibernate, Many to Many relationship query
我有兩個bean類Student和Course,它們之間有很多關系。 例如。 一個學生可以注冊多個課程,反之亦然。 我已經使用HibernateTemplate將對象保存到Oracle DB中。 以下是學生,課程和StudentDao課程。
學生班
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;
}
}
學生道課
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);
}
}
我有以下兩個查詢
我能夠成功將數據保存在student_course表中。 我想知道我是否要從student_course表中檢索數據,如何使用HibernateTemplate進行處理,或者還有其他方法可以這樣做? 例如,查詢就像
select course_id from student_course where student_id=1
請注意,我只希望課程ID列不完整。
如果我要在student_course表中再增加一列,例如course_name(來自課程表),該怎么辦?
您可以通過以下方式進行操作,但是請記住,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();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.