Hibernate关系映射---单向多对一增删查改实例
实际情况:一个班级中有多个学生,一个学生只属于一个班级,学生和班级是多对一的关系
1、新建一个Java工程,添加hibernate3.3支持,连接test数据库
2、在src下新建com.etc.dao包,在包下新建Cls.java类:
package com.etc.dao;
public class Cls {
private int cno;
private String cname;
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Cls [cno=" + cno + ", cname=" + cname + "]";
}
}
3、在com.etc.dao下新建Student.java类:
package com.etc.dao;
public class Student {
private int sno;
private String sname;
private Cls cls;//在多端配置一端的对象
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Cls getCls() {
return cls;
}
public void setCls(Cls cls) {
this.cls = cls;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", cls=" + cls
+ "]";
}
}
4、在com.etc.dao下新建Cls.hbm.xml映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.etc.dao.Cls" table="t_cls" catalog="test">
<id name="cno">
<column name="cls_no"/>
<generator class="native"/>
</id>
<property name="cname">
<column name="cls_name"/>
</property>
</class>
</hibernate-mapping>
5、在com.etc.dao下新建Student.hbm.xml映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.etc.dao.Student" table="t_student" catalog="test">
<id name="sno">
<column name="student_no"/>
<generator class="native"/>
</id>
<property name="sname">
<column name="student_name"/>
</property>
<!-- 在多端配置一端的对象,使用many-to-one -->
<many-to-one name="cls" class="com.etc.dao.Cls">
<column name="cls_no"></column>
</many-to-one>
</class>
</hibernate-mapping>
6、配置hibernate.cfg.xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- <property name="hbm2ddl.auto">create</property> -->
<property name="current_session_context_class">thread</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
<mapping resource="com/etc/dao/Cls.hbm.xml"/>
<mapping resource="com/etc/dao/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7、在工程下新建test文件夹,在文件夹下新建com.etc.dao包,在包下新建StudentDAOTest.java测试类:
package com.etc.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class StudentDAOTest {
/**
* Description:新增:多端操作一端,增加学生的同时,增加该学生所在的班级
* @author zoey
* @date 2017年7月27日
*/
@Test
public void testCreate(){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s1 = new Student();
s1.setSname("张三");
Student s2 = new Student();
s2.setSname("李四");
Cls cls = new Cls();
cls.setCname("开发部");
//设置学生所在的班级
s1.setCls(cls);
s2.setCls(cls);
//保存班级和学生
session.save(cls);
session.save(s1);
session.save(s2);
session.getTransaction().commit();
}
/**
* Description:查询:多端操作一端,查询到指定学生的信息之后,再查询该学生所在的班级信息
* @author zoey
* @date 2017年7月27日
*/
@Test
public void testRetrieve(){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s1 = (Student)session.get(Student.class,1);
System.out.println(s1);
Cls cls = s1.getCls();
System.out.println(cls);
session.getTransaction().commit();
}
/**
* Description:修改:多端操作一端:修改指定学生的信息之后,再修改该学生所在的班级信息
* @author zoey
* @date 2017年7月27日
*/
@Test
public void testUpdate(){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s1 = (Student)session.get(Student.class,1);
s1.setSname("王五");
s1.getCls().setCname("技术部");
session.getTransaction().commit();
}
/**
* Description:删除:删除学生,但是不删除学生所在的班级,是正确的做法,不需要配置cascade=all
* @author zoey
* @date 2017年7月27日
*/
@Test
public void testDelete(){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s1 = (Student)session.get(Student.class,1);//查询id为1的学生
session.delete(s1);//删除id为1的学生
session.getTransaction().commit();
}
}
总结:
1、多对一关系:操作的是多端,所以配置的也是一端,在多端的java类中需要加入一端的对象,在多端的映射文件中需要配置many-to-one,测试类中所有的增删查改操作,首先操作的都是多端,查询的也首先是多端。
2、多对一关系中不需要配置格外的属性,比如cascade=all或者fetch=join等。
3、这里是手动编写类和映射文件,然后在hibernate.cfg.xml中配置如下属性,使得hibernate为我们自动在指定的数据库中创建相应的表。
<property name="hbm2ddl.auto">create</property>
这个属性,只需要在调用testCreate()的时候执行一次便可,执行完这个方法之后,就可以将配置文件中的该语句注释掉,否则会一直删除表,再创建表。