gpt4 book ai didi

java - Hibernate:将记录插入表(一对多关系)

转载 作者:行者123 更新时间:2023-11-30 02:41:10 25 4
gpt4 key购买 nike

我的数据库中有以下表格。

类(class)表

----------------------
|course_id|coursename|
----------------------
| 1 | java |
----------------------
| 2 | c++ |
----------------------

类(class)_学生表

----------------------
|course_id|student_id|
----------------------

学生表

------------------------
|student_id|studentname|
------------------------

我使用的关系是一对多。学生可以注册许多类(class)。

我想要实现的是,如果一个学生(jane)注册了一门类(class),该记录将被插入到 Student 和 Course_Student 表中。如果同一个学生(jane)注册了另一门类(class),则学生记录不会插入到Student表中,而只会插入到Course_Student表中。

由于我是 hibernate 新手,我不太确定如何实现上述要求。请帮我。谢谢。

学生POJO类

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
int student_id;
@Column
String studentname;

public Student(int student_id, String studentname) {
this.student_id = student_id;
this.studentname = studentname;
}

public int getStudent_id() {
return student_id;
}

public void setStudent_id(int student_id) {
this.student_id = student_id;
}

public String getStudentname() {
return studentname;
}

public void setStudentname(String studentname) {
this.studentname = studentname;
}

@Override
public String toString() {
return "Student [student_id=" + student_id + ", studentname=" + studentname + "]";
}
}

类(class)POJO类

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

public class Course {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
int course_id;
@Column
String coursename;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
List<Student> student = new ArrayList<Student>();

public Course(int course_id, String coursename, List<Student> student) {
this.course_id = course_id;
this.coursename = coursename;
this.student = student;
}
public int getCourse_id() {
return course_id;
}
public void setCourse_id(int course_id) {
this.course_id = course_id;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}

@Override
public String toString() {
return "Course [course_id=" + course_id + ", coursename=" + coursename + ", student=" + student + "]";
}
}

HibernateUtils 类

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
private static SessionFactory sessionFactory = null;

private static SessionFactory getSessionFactory() {
if(sessionFactory==null) {
//Locate and read hibernate.cfg.xml file default in src/main/java
sessionFactory = new Configuration().configure().buildSessionFactory();
return sessionFactory;


}
else {
return sessionFactory;
}
}

public static Session getCurrentSession() {
return getSessionFactory().getCurrentSession();
}

public static Session openSession() {
return getSessionFactory().openSession();
}

public static void closeSession(Session session) {
if(session.isOpen()) {
session.clear();
}
}
}

应用程序类

import org.hibernate.Session;

public class Application {

//a student can apply many courses
public static void applyCourse(String coursename, String studentname) {
Session session = HibernateUtils.openSession();
session.beginTransaction();
Course course = session.get(Course.class, coursename);

//not sure how to implement this part please help
for(int i =0; i < course.getStudent().size(); i++) {
if(course.getStudent().get(i).getStudentname().equals(studentname)) {
//if student record exist,add to Course_Student table but don't add to Student table
}
else {
//if student record doesn't exist,add to Course_Student table and Student table
}
}
session.save(course);
HibernateUtils.closeSession(session);
}


public static void main(String[] args) {
Application.applyCourse("java", "jane");
Application.applyCourse("c++", "jane");

}
}

最佳答案

您的关联是多对多的典型示例:每个学生注册多个类(class),每个类(class)由多个学生参加。所以应该是ManyToMany,而不是OneToMany。

此外,cascade=ALL 在这种情况下没有意义:如果您删除类(class),您不希望所有参加该类(class)的学生也被删除。您应该删除级联。

另外:

List<Student> student

这是一个列表。其中包含数名学生。所以你应该将其命名为 students ,不是student 。命名student非常令人困惑:我给人的感觉是一门类(class)是由一个学生参加的。选择更好的名字。同一学生不应该多次参加同一门类(class),并且类(class)中学生的顺序并不重要,因此您应该使用 Set<Student>而不是List<Student>

Course course = session.get(Course.class, coursename);

这是不正确的。 Session.get() 将唯一标识符作为参数。不是类(class)名称。如果是类(class),则需要传递 ID,而不是其名称。或者您需要一个查询(使用 HQL)来查找具有该名称的类(class)。

    //not sure how to implement this part please help
for(int i =0; i < course.getStudent().size(); i++) {
if(course.getStudent().get(i).getStudentname().equals(studentname)) {
//if student record exist,add to Course_Student table but don't add to Student table
}
else {
//if student record doesn't exist,add to Course_Student table and Student table
}
}

这不是正确的逻辑。你不应该只在类(class)的学生中寻找学生。否则,您只会找到已经注册该类(class)的学生。最有可能的是,该学生已经存在,但尚未注册该类(class)。在这种情况下,您不应该重新创建学生。所以正确的逻辑是

Student student = findStudentByName(studentName);
if (student == null) {
student = createStudent(studentName);
}
course.addStudent(student);

请注意,即使学生已经存在并且已经参加了类(class),将其再次添加到类(class)中也不是问题:因为集合是 Set<Student> ,再次添加不会修改集合。

关于java - Hibernate:将记录插入表(一对多关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com