`

hibernate 映射关系

 
阅读更多

员工和部门对象创建如下:

publicclass Employee {

privateintid;

private String name;

private Department depart;

}

publicclass Department {

privateintid;

private String name;

}

Employee.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="Employee">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<!-- 映射属性和字段 -->

<property name="name" column="NAME" length="50" />

<many-to-one name="depart" column="depart_id" ></many-to-one>

</class>

</hibernate-mapping>

Department.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="Department" table="Department">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<!-- 映射属性和字段 -->

<property name="name" column="NAME" length="50" />

<!-- 集合放的class -->

<set name="emps">

<key column="depart_id"></key>

<one-to-many class="Employee"/>

</set>

</class>

</hibernate-mapping>

Employee 外键depart 来自 Department表的主键id;

Many to One 多个员工对应一个部门

需在类Employee 中添加 depart属性

private Department depart;

创建getset方法

在多的一方Employee.hbm.xml 文档中创建

<many-to-one name="depart" column="depart_id" ></many-to-one>

name 指的是类中的属性

使用方法

static Department add()

{

Session s = null;

Transaction tx = null;

try{

Department depart = new Department();

depart.setName("depart name");

Employee emp = new Employee();

emp.setDepart(depart);//对象模型,建立两个对象关系

emp.setName("emp name");

s = HibernateUtil.getSession();

tx = s.beginTransaction();

/*s.save(depart);

s.save(emp);*/

//save顺序没关系,多了条update语句

s.save(emp);

s.save(depart);

tx.commit();

return depart;

}finally{

if(s != null)

s.close();

}

}

One to Many 一个部门对应多个员工

Department类中添加

private Set<Employee> emps; 并创建GetSet方法。

Department.hbm.xml 中配置

<set name="emps">

<!-- 对应外键名称 -->

<key column="depart_id"></key>

<one-to-many class="Employee"/>

</set>

测试代码

static Department add()

{

Session s = null;

Transaction tx = null;

try{

Department depart = new Department();

depart.setName("depart name");

Employee emp1 = new Employee();

emp1.setDepart(depart);//对象模型,建立两个对象关系

emp1.setName("emp name1");

Employee emp2 = new Employee();

emp2.setDepart(depart);//对象模型,建立两个对象关系

emp2.setName("emp name2");

s = HibernateUtil.getSession();

tx = s.beginTransaction();

s.save(emp1);

s.save(emp2);

s.save(depart);

tx.commit();

return depart;

}finally{

if(s != null)

s.close();

}

}

static Department query(int departId)

{

Session s = null;

try{

s = HibernateUtil.getSession();

Department depart = (Department) s.get(Department.class, departId);

System.out.println("emp size:" + depart.getEmps().size());

return depart;

}finally{

if(s != null)

s.close();

}

}

基于主键One to One 主对象从对象

publicclass Person {

privateintid;

private String name;

private IdCard idCard;

}

publicclass IdCard {

privateintid;

private Date usefulLife;

private Person person;

}

Person.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="Person" table="Person">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<!-- 映射属性和字段 -->

<property name="name" column="NAME" length="50" />

<one-to-one name="idCard"></one-to-one>

</class>

</hibernate-mapping>

IdCard.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="IdCard" table="id_card">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="foreign">

<param name="property">person</param>

</generator>

</id>

<!-- 映射属性和字段 -->

<property name="usefulLife" column="usefulLife" length="50" />

<one-to-one name="person" constrained="true"></one-to-one>

</class>

</hibernate-mapping>

constrained="true" 该属性等于true,约束表中的外键关系。

测试用例:

static Person add()

{

Session s = null;

Transaction tx = null;

try{

s = HibernateUtil.getSession();

IdCard idCard = new IdCard();

idCard.setUsefulLife(new Date());

Person p = new Person();

p.setName("p1");

p.setIdCard(idCard);

//这句话很关键,起到连接的作用

idCard.setPerson(p);

tx = s.beginTransaction();

s.save(p);

s.save(idCard);

tx.commit();

return p;

}finally

{

if(s != null)

{

s.close();

}

}

}

可以只保存Person对象,但要保存idCard对象,一定要设置

idCard.setPerson(p);

基于外键的One to One

与多对一类似,需要加上唯一约束

IdCard.hbm.xml 修改

<generator class="native"></generator>

<many-to-one name="person" column=”person_id” unique="true"></many-to-one>

Person.hbm.xml 修改

<one-to-one name="idCard" property-ref="person"></one-to-one>

多对多 many to many

老师和学生的多对多的关系

publicclass Teacher {

privateintid;

private String name;

private Set<Student> students;

}

Teacher.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="Teacher">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<!-- 映射属性和字段 -->

<property name="name" column="NAME" length="50" />

<!-- 集合放的class -->

<set name="students" table="teacher_student">

<key column="teacher_id"></key>

<many-to-many class="Student" column="student_id"/>

</set>

</class>

</hibernate-mapping>

我们最终的目的是:获取student表信息。

中间有一张关联表,首先通过teacher_id,获取teacher_student表信息,然后通过student_id获取student表信息。

Student

publicclass Student {

privateintid;

private String name;

private Set<Teacher> teachers;

}

Student.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="Student">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<!-- 映射属性和字段 -->

<property name="name" column="NAME" length="50"/>

<!-- 集合放的class -->

<set name="teachers" table="teacher_student">

<key column="student_id" />

<many-to-many class="Teacher" column="teacher_id"/>

</set>

</class>

</hibernate-mapping>

测试代码:

老师来维护中间表操作

package cn.hibernate.test;

import java.util.HashSet;

import java.util.Set;

import org.hibernate.Session;

import org.hibernate.Transaction;

import util.HibernateUtil;

import cn.hibernate.pojo.Student;

import cn.hibernate.pojo.Teacher;

public class Many2Many {

public static void main(String[] args) {

add();

}

static void add()

{

Session s = null;

Transaction tx = null;

try{

Set<Student> ss = new HashSet<Student>();

Teacher t1 = new Teacher();

t1.setName("t1 name");

Teacher t2 = new Teacher();

t2.setName("t2 name");

Student s1 = new Student();

s1.setName("s1 name");

Student s2 = new Student();

s1.setName("s2 name");

ss.add(s1);

ss.add(s2);

//老师维护关系,插入数据,不能两方来维护。

t1.setStudents(ss);

t2.setStudents(ss);

s = HibernateUtil.getSession();

tx = s.beginTransaction();

s.save(t1);

s.save(t2);

s.save(s1);

s.save(s2);

tx.commit();

}finally{

if(s != null)

{

s.close();

}

}

}

}

学生维护中间表关系

package cn.hibernate.test;

import java.util.HashSet;

import java.util.Set;

import org.hibernate.Session;

import org.hibernate.Transaction;

import util.HibernateUtil;

import cn.hibernate.pojo.Student;

import cn.hibernate.pojo.Teacher;

publicclass Many2Many {

publicstaticvoid main(String[] args) {

add();

}

staticvoid add()

{

Session s = null;

Transaction tx = null;

try{

Set<Teacher> ts = new HashSet<Teacher>();

Teacher t1 = new Teacher();

t1.setName("t1 name");

Teacher t2 = new Teacher();

t2.setName("t2 name");

ts.add(t1);

ts.add(t2);

Student s1 = new Student();

s1.setName("s1 name");

Student s2 = new Student();

s1.setName("s2 name");

//学生维护中间表关系

s1.setTeachers(ts);

s2.setTeachers(ts);

s = HibernateUtil.getSession();

tx = s.beginTransaction();

s.save(s1);

s.save(s2);

s.save(t1);

s.save(t2);

tx.commit();

}finally{

if(s != null)

{

s.close();

}

}

}

}

代码中只能由一方维护数据,不可双方共同维护,否则,插入重复异常。

两个对象之间的关联组件关系映射

public class Name(){

private String firstName;

private String lastName;

}

public class User(){

private int id;

private Name name;

}

User.hbm.xml

<hibernate-mapping package="cn.hibernate.pojo">

<!-- 映射类和数据表 -->

<class name="User" table="User">

<!-- 映射 OID 数据表的主键 -->

<id name="id" column="ID">

<generator class="native" />

</id>

<component name="name">

<property name="firstName" column="first_name" type="string"/>

<property name="lastName" column="last_name" type="string"/>

</component>

<property name="age" column="age" type="integer"/>

</class>

</hibernate-mapping>

测试代码:

staticvoid add()

{

Session s= null;

Transaction tx = null;

try{

s = HibernateUtil.getSession();

tx = s.beginTransaction();

Name name = new Name();

name.setFirstName("firstName");

name.setLastName("lastName");

User user = new User();

user.setName(name);

user.setAge(18);

s.save(user);

tx.commit();

}finally{

if(s != null)

{

s.close();

}

}

}

一对一缺省情况下,关联查。其他分两次查。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics