ManyToOne
hibernet.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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="po/Employee.hbm.xml"></mapping>
<mapping resource="po/Department.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Employee
package po;
public class Employee {
private int employeeId;
private String ename;
private Department department;
//省略get、set和无参构造
}
Employee.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 package="po">
<class name="Employee" table="t_employee">
<id name="employeeId" column="id">
<generator class="native"></generator>
</id>
<property name="ename" column="ename"></property>
<many-to-one name="department" class="Department" cascade="all">
<column name="deptId"></column>
</many-to-one><!--传说中的外键。-->
</class>
</hibernate-mapping>
Department
package po;
public class Department {
private int deptId;
private String dname;
private String location;
//省略get、set和无参构造
}
Department.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 package="po">
<class name="Department" table="t_department">
<id name="deptId" column="id">
<generator class="native"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="location" column="location" length="40"></property>
</class>
</hibernate-mapping>
EmployeeDao 在这个类中做测试。
package dao;
import org.hibernate.Session;
import po.Department;
import po.Employee;
import util.HibUtil;
public class EmployeeDao {
public void add(Employee employee){
Session session = HibUtil.getSession();
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
session.close();
}
public static void main(String[] args) {
EmployeeDao dao = new EmployeeDao();
Employee employee = new Employee();
employee.setEname("wang");
Department department = new Department();
department.setDname("项目部");
department.setLocation("三楼");
employee.setDepartment(department);
dao.add(employee);
}
}
OneToMany只修改department.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 package="po">
<class name="Department" table="t_department">
<id name="deptId" column="id">
<generator class="native"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="location" column="location"></property>
<set name="employees" cascade="all">
<key>
<column name="deptId"></column>
</key>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
经试验发现有了one-to-mang之后,mang-to-one就没用了。基本得出结论啊:无论是one-to-mang还是mang-to-one的使用都是双向的任何一个的使用都可以建立起来双向的连接。