使用Hibernate注解开发学生选课系统、其中共有4张表:
1、课程表Course:表明存在哪些课程。
2、学生表Student:记录学生信息。
3、选课表sc:此表为学生选课的记录,维护学生和课程的多对多关系。
4、教师表teacher:记录教师信息,一个老师只教一门课,但一门课程可以由多个老师教授,与课程表为一对多的关系。
开发流程:
导入Hibernate相关jar包
image.png
2.配置hibernate的核心文件hibernate.cfg.xml
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/spring?characterEncoding=utf8
root
1234
10
true
true
update
org.hibernate.dialect.MySQL5InnoDBDialect
3.编写实体类
Course:
package com.lj.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
@Entity
public class Course {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany
@JoinTable(name="sc",
joinColumns=@JoinColumn(name="cid"),
inverseJoinColumns=@JoinColumn(name="sid"))
private Set students = new HashSet<>();
//mappedBy表示此表为从表,关系由对方维护相当于inverse="true"
@OneToMany(cascade=CascadeType.ALL,mappedBy="course")
private Set teachers = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
public Set getTeachers() {
return teachers;
}
public void setTeachers(Set teachers) {
this.teachers = teachers;
}
}
Teacher:
package com.lj.entity;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Teacher {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="course_id")
private Course course;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course getCourse () {
return course;
}
public void setCourse (Course course) {
this.course = course;
}
}
Student:
package com.lj.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany
//设置级联操作
@Cascade(CascadeType.SAVE_UPDATE)
//配置中间表sc,joinColumns为本表在sc的字段,inverseJoinColumns为对方表在sc的字段
@JoinTable(name="sc",
joinColumns=@JoinColumn(name="sid"),
inverseJoinColumns=@JoinColumn(name="cid"))
private Set courses = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
}
注意在使用注解的方法开发给entity类添加注解的时候一定注意注解要么全写在属性上要么全写在get方法上一定不要混着写否则会出现以下错误:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table...
编写测试类:
public class ManyTableTest {
private Configuration cfg;
private SessionFactory sessionFactory;
private Session session;
private Transaction tx;
@Before
public void common(){
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
}
@Test
public void testManyToMany(){
Teacher teacher1 =new Teacher();
teacher1.setName("张老师");
Teacher teacher2 = new Teacher();
teacher2.setName("李老师");
Course course1 = new Course();
course1.setName("语文");
teacher1.setCourse(course1);
Course course2 = new Course();
course2.setName("数学");
Student student1 = new Student();
student1.setName("小明");
Student student2 = new Student();
student2.setName("小刚");
//小明选了语文和数学课
student1.getCourses().add(course1);
student1.getCourses().add(course2);
//数学课添加了小刚
course2.getStudents().add(student2);
session.save(teacher1);
session.save(teacher2);
session.save(course1);
session.save(course2);
session.save(student1);
session.save(student2);
}
@After
public void after(){
tx.commit();
session.close();
sessionFactory.close();
}
}