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

最新文章

  1. Smart3D系列教程6之 《案例实战演练3——倾斜数据正射影像及DSM的生产》
  2. WebPack常用功能介绍
  3. jQuery lazyload 懒加载
  4. linux 接口地址全部清除才清理从此接口发出的下一跳路由
  5. WSUS更新服务器
  6. 文件操作_菜单&lt;代码&gt;
  7. JAVA 打印指定月份日历
  8. NodeJS常用工具
  9. Filestream读取或写入文件
  10. BZOJ 2151 种树
  11. MySQL(root用户)密码重置
  12. Java历程-初学篇 Day03扫描仪与类型转换
  13. MySQL之SELECT用法
  14. ORACLE数据库学习之SQL性能优化详解
  15. matlab下K-means Cluster 算法实现
  16. Java8新特性之二:方法引用
  17. groupcache源码解析-概览
  18. Matlab-6:解非线性方程组newton迭代法
  19. python 编码 自动加双斜杠问题
  20. vue学习(1)

热门文章

  1. redis使用测试
  2. python爬虫(4)--Cookie的使用
  3. JavaScript的流程控制语句
  4. 经典的CSS代码(转)
  5. oracle创建完实例删除的时候报ORA-01031:insufficient privileges错误,解决办法
  6. Python 安装 django框架
  7. SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数
  8. activeMQ功能Demo
  9. bzoj4619 4619: [Wf2016]Swap Space
  10. loj10100 网络