HIbernate | 简单对象嵌套实践
2024-09-03 06:31:37
src/entity/Course.java
public class Course {
private Integer courseNo;
private String courseName;
public Integer getCourseNo() {
return courseNo;
}
public void setCourseNo(Integer courseNo) {
this.courseNo = courseNo;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
} }
src/entity/Student.java
public class Student {
private Integer id;
private String studentNo;
private String name;
private Course course;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
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;
} }
src/entity/Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity">
<class name="Student" table="student_table">
<id name="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="studentNo" />
<property name="name" /> <many-to-one name="course" cascade="all" class="Course" column="course_no" /> </class> </hibernate-mapping>
src/entity/Course.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity">
<class name="Course" table="course_table">
<id name="courseNo" column="course_no">
<generator class="identity"/>
</id>
<property name="courseName" />
</class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="entity/Student.hbm.xml"/>
<mapping resource="entity/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类
Session session = hibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
Course course = new Course();
course.setCourseName("蛋疼的软件测试");
student.setName("wade");
student.setStudentNo("87937128379821");
student.setCourse(course); session.persist(student);
tx.commit();
hibernateUtil.closeSession();
最新文章
- Smart3D系列教程6之 《案例实战演练3——倾斜数据正射影像及DSM的生产》
- WebPack常用功能介绍
- jQuery lazyload 懒加载
- linux 接口地址全部清除才清理从此接口发出的下一跳路由
- WSUS更新服务器
- 文件操作_菜单<;代码>;
- JAVA 打印指定月份日历
- NodeJS常用工具
- Filestream读取或写入文件
- BZOJ 2151 种树
- MySQL(root用户)密码重置
- Java历程-初学篇 Day03扫描仪与类型转换
- MySQL之SELECT用法
- ORACLE数据库学习之SQL性能优化详解
- matlab下K-means Cluster 算法实现
- Java8新特性之二:方法引用
- groupcache源码解析-概览
- Matlab-6:解非线性方程组newton迭代法
- python 编码 自动加双斜杠问题
- vue学习(1)