创建部门表department
创建雇员表employee
Department.java
package www.hbsi.net.many2one; public class Department { private Integer id; private String name; public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Employee.java
package www.hbsi.net.many2one; public class Employee { private Integer id; private Department department;//反应了对象之间的关系 private String username; public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Department getDepartment() { return this.department; } public void setDepartment(Department department) { this.department = department; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } }
Department.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 package="www.hbsi.net.many2one"> <class name="Department" table="department" catalog="hibernate"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="name" column ="name" type="string" /> </class> </hibernate-mapping>
Employee.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 package="www.hbsi.net.many2one"> <class name="Employee" table="employee"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="username" type="string" column ="username" /> <many-to-one name="department" column ="dept_id" /> </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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="www/hbsi/net/many2one/Department.hbm.xml" /> <mapping resource="www/hbsi/net/many2one/Employee.hbm.xml" /> </session-factory> </hibernate-configuration>
Many2one.java
package www.hbsi.net.many2one; import javax.persistence.Temporal; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class Many2one { @Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Department department = new Department(); department.setName("软件系"); Employee employee1 = new Employee(); employee1.setUsername("Nacy"); employee1.setDepartment(department); Employee employee2 = new Employee(); employee2.setUsername("Tom"); employee2.setDepartment(department); session .save(department); session .save(employee1); session .save(employee2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
department表
employee表
Many2one.java
/** * 先保存部门在保存雇员 * 一般推荐这种方法 */ package www.hbsi.net.many2one; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class Many2one { @Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Department department = new Department(); department.setName("软件系"); Employee employee1 = new Employee(); employee1.setUsername("Nacy"); employee1.setDepartment(department); Employee employee2 = new Employee(); employee2.setUsername("Tom"); employee2.setDepartment(department); session .save(department); session .save(employee1); session .save(employee2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } /** * 通过雇员ID查找所属部门 */ @Test public void find() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Employee employee = (Employee) session.get(Employee.class, 5); System.out.println(employee.getUsername()+"--"+employee.getDepartment().getName()); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }