浏览 3454 次
锁定老帖子 主题:Hibernate映射引用属性
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-01-07
最后修改:2009-01-07
Hibernate的基本映射
映射引用属性 引用属性的意思是:持久化类的属性既不是基本数据类型,也不是String字符串, 而是某个引用变量,该引用属性的类型可以是自定义类。 先看-->POJO源代码: package com; public class Person implements Serialiazable { private int id; private int age; private Name name; person() {} public void setId(int id) { this.id = id; } public int getId() { return id; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(Name name) { this.name = name; } public Name getName() { return name; } } 注意:由于数据库的列无法存储Name对象,因此无法直接使用property映射name属性。为了映射引用属性, Hibernate提供了 [color=red]component元素。每个component元素映射一个引用属性,引用属性必须指定该属性的类型。因此componet元素要求具有class 属性,该属性用于确定引用属性的类型。[/color] 接着看-->持久化映射文件Person.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="com"> <class name="Person" table="person"> <!-- id属性映射标识属性 --> <id name = "id" column="person_id"> <!-- 指定主键生成器策略 --> <generator class="increment"/> </id> <property name="age"/> <!--映射引用属性name,引用属性的类型为Name--> <component name="name" class="Name" unique="true"> <!--映射引用属性的first属性--> <property name="first"/> <!--映射引用属性的last属性--> <property name="last"/> </componet> </class> </hibernate-mapping> 引用属性还有如下两种特殊的情况:
下面对这两种情况具体分析。 一、集合引用属性的映射 1.先看POJO-->>源代码: package com; public class Person implements Serializabale { private int id; private String name; private int age; private List schools = new ArrayList(); Person() {} public void setSchools(List schools) { this.schools = schools; } public List getSchools() { return schools; } public void setId(int id) { this.id = id; } publlic int getId() { return id; } public void setName(String name) { this.name = name; } publlic String getName() { return name; } public void setAge(int age) { this.age = age; } publlic int getAge() { return age; } } 2.School类 前面Person实例的schools属性里存放系列的字符串,而现在的schools属性里存放系列的School实例。 package com; public class School implements Serialiazable { //School类的两个属性:name和address private String name; private String address; public School() {} public School(String s1, String s2) { this.name = s1; this.address = s2; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } …… } 3.Person.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="com"> <class name="Person"> <id name = "id" column="personid"> <generator class="increment"/> </id> <property name="name"/> <property name="age"/> <!--映射List集合属性--> <list name="schools" table="school"> <!--映射关联外键列--> <key column="personid" not-null="true"/> <!--List有序集合,需要索引列--> <list-index column="list_order"/> <!--composite-element映射集合里的元素,class属性确定集合里元素的类型--> <composite-element class="School"> <!--每个property属性映射集合元素的基本属性--> <property name="name"/> <property name="address"/> </composite-element> </list> </class> </hibernate-mapping> 二、引用类型主键的映射 1.POJO package com; public class Person { //用作持久化类Person的标识属性 private Name name; //普通属性age private int age; Person() {} …… } 2.如果持久化类需要使用引用类型作为表示属性时,则该类应满足以下两个条件:
//标识属性类,实现Serializable接口 package com; public class Name implements Serializable { private String firstName; private String lastName; public Name(){} …… //重写hashCode()方法,该方法根据firstName和lastName的值计算得到 public int hashCode() { return firstName.hashCode() + lastName.hashCode(); } //重写equals方法,同样也根据firstName和lastName两个属性来判断 public boolean equals(Object o) { if (o instanceof Name) { Name p = (Name)o; if (p.getFirstName().equals(firstName) && p.getLastName().equals(lastName)) { return true; } else{ return false; } } else{ return false; } } } 3.POJO Person持久化映射文件: <?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="prolove"> <class name="Person" table="person"> <!--composite-id元素用于映射引用类型的标识属性,其中class元素确定属性的类型--> <composite-id name="name" class="Name"> <!--key-property元素确定标识属性类包含的属性--> <key-property name="firstName"/> <key-property name="lastName"/> </composite-id> <property name="age"/> </class> </hibernate-mapping> ==>建议尽量不要使用这种复杂的标识属性。关于联合主键的映射还有一种策略,直接 将多个属性映射成数据库主键 三、复合主键的映射 改写上面的持久化类Person,不使用name作为Person的标识属性,而是直接使用firstName和lastName作为标识属性 映射复合主键的持久化类必须满足以下2个条件:
1.POJO Person package com; public class Person implements Serializable { //直接定义lastName和firstName两个属性 private String lastName; private String firstName; private int age; Person() {} ……省略setter&getter public boolean equals(Object o) { if (o instanceof Person) { Person p = (Person)o; if (p.getFirstName().equals(firstName) && p.getLastName().equals(lastName)){ return true; } else{ return false; } } else{ return false; } } public int hashCode() { int hashCode = firstName.hashCode() + lastName.hashCode(); return hashCode; } } 2.映射文件源代码 <?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="prolove"> <class name="Person" table="personcomid"> <!--composite-id元素用于映射复合主键--> <composite-id> <!--key-property元素映射复合主键的每个元素--> <key-property name="firstName"/> <key-property name="lastName"/> </composite-id> <property name="age"/> </class> </hibernate-mapping> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |