1.新建工程项目hibernate_one2many_1,在该项目下添加hibernate所需的Jar包和数据库驱动mysql驱动Jar包。
2.编写工具类HibernateUtils和ExportDB,代码如下:
HibernateUtils.java
- package com.i51pro.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtils {
- private static SessionFactory sessionFactory;
- static {
- try {
- Configuration cfg = new Configuration().configure();
- sessionFactory = cfg.buildSessionFactory();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static Session getSession() {
- return sessionFactory.openSession();
- }
- public static void closeSession(Session session) {
- if (session != null) {
- if (session.isOpen()) {
- session.close();
- }
- }
- }
- }
ExportDB.java
- package com.i51pro.hibernate;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class ExportDB {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Configuration cfg = new Configuration().configure();
- SchemaExport export = new SchemaExport(cfg);
- export.create(true, true);
- }
- }
3.编写实体类和映射文件,代码如下:
Student.java
- package com.i51pro.hibernate;
- public class Student {
- private int id;
- private String name;
- 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;
- }
- }
Classes.java
- package com.i51pro.hibernate;
- import java.util.Set;
- public class Classes {
- private int id;
- private String name;
- private Set students;
- 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;
- }
- }
Student.hbm.xml
- <?xml version="1.0"?>
- <!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.i51pro.hibernate.Student" table="t_student">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="name" />
- </class>
- </hibernate-mapping>
Classes.hbm.xml
- <?xml version="1.0"?>
- <!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.i51pro.hibernate.Classes" table="t_classes">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name"/>
- <set name="students">
- <key column="classesid" />
- <one-to-many class="com.i51pro.hibernate.Student"/>
- </set>
- </class>
- </hibernate-mapping>
4.编写hibernate映射文件,代码如下:
hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2many_1</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">admin</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.show_sql">true</property>
- <mapping resource="com/i51pro/hibernate/Student.hbm.xml"/>
- <mapping resource="com/i51pro/hibernate/Classes.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
5.在项目hibernate_one2many下新建SourceFolder,命名为test,在该包下编写测试类One2Many.java,进行单元测试,它的代码如下:
One2Many.java
- package com.i51pro.hibernate;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import junit.framework.TestCase;
- public class One2ManyTest extends TestCase {
- public void testSave1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Transaction tx = session.beginTransaction();
- Student student1 = new Student();
- student1.setName("学生1");
- session.save(student1);
- Student student2 = new Student();
- student2.setName("学生2");
- session.save(student2);
- Set students = new HashSet();
- students.add(student1);
- students.add(student2);
- Classes classes = new Classes();
- classes.setName("JAVA学习小组");
- classes.setStudents(students);
- //可以保存
- session.save(classes);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testLoad1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Transaction tx = session.getTransaction();
- Classes classes = (Classes)session.load(Classes.class, 1);
- System.out.println("classes.name:" + classes.getName());
- Set students = classes.getStudents();
- for (Iterator iter=students.iterator(); iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println("student.name=" + student.getName());
- }
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
总结:hihernate一对多关联映射(单向Classes----->Student)
一对多关联映射利用了多对一关联映射原理
多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一
一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多
也就是说一对多和多对一的映射策略是一样的,只是站的角度不同
在一一端维护关系的缺点:
* 如果将t_studen不是在student这一t表里的classesid字段设置为非空,则无法保存
* 因为端维护关系,所以student不知道是哪个班的,
所以需要发出多余的update语句来更新关系