![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.deuask.one2many; import java.util.HashSet; import java.util.Set; public class Deptment { private Integer id; private String deptname; private Set<Employ> employs=new HashSet<Employ>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<Employ> getEmploys() { return employs; } public void setEmploys(Set<Employ> employs) { this.employs = employs; } @Override public String toString() { return "[所在部门: id=" + id + ", name=" + deptname + "]"; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.deuask.one2many; public class Employ { private Integer id; private String name; private Deptment deptment; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Deptment getDeptment() { return deptment; } public void setDeptment(Deptment deptment) { this.deptment = deptment; } @Override public String toString() { return "[员工: id=" + id + ", name=" + name + "]"; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.deuask.one2many; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static{ //加载hibernate主配置文件 Configuration confg=new Configuration(); confg.configure("com/deuask/one2many/hibernate.cfg.xml"); sessionFactory=confg.buildSessionFactory(); } //创建session public static Session getSession(){ return sessionFactory.openSession(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.deuask.one2many; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; public class TestApp { @Test public void testSave(){ //构建对象 Deptment dept=new Deptment(); dept.setDeptname("研发2部"); Employ emp1=new Employ(); emp1.setName("张三2"); Employ emp2=new Employ(); emp2.setName("李四2"); dept.getEmploys().add(emp1); dept.getEmploys().add(emp2); emp1.setDeptment(dept); emp2.setDeptment(dept); //得到session Session session=HibernateUtil.getSession(); Transaction tr=session.beginTransaction(); session.save(emp1); session.save(emp2); session.save(dept); tr.commit(); session.close(); } @Test public void getTest(){ //通过部门查找到所有的员工 Session session=HibernateUtil.getSession(); Deptment deptment=(Deptment)session.get(Deptment.class, 1); System.out.println("部门:"+deptment.getId()+" "+deptment.getDeptname()); System.out.println(deptment.getEmploys()); } @Test public void getTest2(){ //通过部门查找到所有的员工 Session session=HibernateUtil.getSession(); //先找到员工 Employ emp=(Employ) session.get(Employ.class, 1); //通过员工找到部门 Deptment dept=emp.getDeptment(); System.out.println(emp.getId()+" "+emp.getName()); System.out.println(dept); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.deuask.one2many"> <class name="com.deuask.one2many.Deptment" table="deptment"> <id name="id"> <generator class="native"></generator> </id> <property name="deptname"></property> <set name="employs" > <key column="deptid"></key> <one-to-many class="com.deuask.one2many.Employ"/> </set> </class> </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.deuask.one2many"> <class name="com.deuask.one2many.Employ" table="employ"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <many-to-one name="deptment" class="com.deuask.one2many.Deptment" column="deptid"></many-to-one> </class> </hibernate-mapping>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!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 name="foo"> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql:///hibernate_20120328</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="hibernate.connection.password">hyy</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">false</property> <!-- create:先删除,再创建 update:如果表不存在就创建,不一样就更新,一样就什么都不做。 create-drop:初始化时创建表,SessionFactory执行close()时删除表。 validate:验证表结构是否一致,如果不一致,就抛异常。 --> <property name="hbm2ddl.auto">update</property> <!-- 设置默认的事务隔离级别: 隔离级别 对应的整数表示 READ UNCOMMITED 1 READ COMMITED 2 REPEATABLE READ 4 SERIALIZEABLE 8 --> <property name="connection.isolation">2</property> <!-- 导入映射文件 --> <mapping resource="com/deuask/one2many/Employee.hbm.xml"/> <mapping resource="com/deuask/one2many/Department.hbm.xml"/> </session-factory> </hibernate-configuration>