Hibernate的学习总结(复合主键)
2011-02-14 15:05
477 查看
当表存在复合主键时,Hibernate对其的处理稍微复杂一些。
1.POJO类得实现Serializable接口
2.POJO类得重写equals()方法和hashCode()方法
3.hbm文件中用<composite-id>节点配置复合主键信息
其中重写equals()方法和hashCode()方法可以使用commons-lang包来简化处理
以下是一个小例子:
其hbm配置文件代码为:
还有一种更好的方式是,将复合主键部分单独提出一个POJO类
person类
PersonKey类
hbm文件
1.POJO类得实现Serializable接口
2.POJO类得重写equals()方法和hashCode()方法
3.hbm文件中用<composite-id>节点配置复合主键信息
其中重写equals()方法和hashCode()方法可以使用commons-lang包来简化处理
以下是一个小例子:
import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Person implements Serializable { private String name; private String phone; private int age; public boolean equals(Object obj) { // TODO 自动生成方法存根 if (this == obj) { return true; } if (!(obj instanceof Person)) { return false; } Person p = (Person) obj; return new EqualsBuilder().append(this.name, p.name).append(this.phone, p.phone).append(this.age, p.age).isEquals(); } public int hashCode() { // TODO 自动生成方法存根 return new HashCodeBuilder().append(this.name).append(this.age).append( this.phone).toHashCode(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
其hbm配置文件代码为:
<?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" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Sat Nov 11 21:30:08 CST 2006 --> <hibernate-mapping package="org.lxh.hibernate02"> <class name="Person" table="PERSON"> <composite-id> <key-property name="name" column="NAME" type="string"/> <key-property name="phone" column="PHONE" type="string"/> </composite-id> <property name="age" column="AGE" type="int" /> </class> </hibernate-mapping>
还有一种更好的方式是,将复合主键部分单独提出一个POJO类
person类
import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Person implements Serializable { private PersonKey id ; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public PersonKey getId() { return id; } public void setId(PersonKey id) { this.id = id; } }
PersonKey类
/* * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. * * Created Sat Nov 11 21:36:22 CST 2006 by MyEclipse Hibernate Tool. */ package org.lxh.hibernate03; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * A class representing a composite primary key id for the PERSON * table. This object should only be instantiated for use with instances * of the Person class. * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. */ public class PersonKey implements Serializable { /** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */ private volatile int hashValue = 0; /** The value of the NAME component of this composite id. */ private java.lang.String name; /** The value of the PHONE component of this composite id. */ private java.lang.String phone; /** * Simple constructor of PersonKey instances. */ public PersonKey() { } /** * Returns the value of the name property. * @return java.lang.String */ public java.lang.String getName() { return name; } /** * Sets the value of the name property. * @param name */ public void setName(java.lang.String name) { hashValue = 0; this.name = name; } /** * Returns the value of the phone property. * @return java.lang.String */ public java.lang.String getPhone() { return phone; } /** * Sets the value of the phone property. * @param phone */ public void setPhone(java.lang.String phone) { hashValue = 0; this.phone = phone; } /** * Implementation of the equals comparison on the basis of equality of the id components. * @param rhs * @return boolean */ public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Person)) { return false; } PersonKey p = (PersonKey) obj; return new EqualsBuilder().append(this.name, p.getName()).append(this.phone, p.getPhone()).isEquals(); } /** * Implementation of the hashCode method conforming to the Bloch pattern with * the exception of array properties (these are very unlikely primary key types). * @return int */ public int hashCode() { return new HashCodeBuilder().append(this.name).append( this.phone).toHashCode(); } }
hbm文件
<?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" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Sat Nov 11 21:36:21 CST 2006 --> <hibernate-mapping package="org.lxh.hibernate03"> <class name="Person" table="PERSON"> <composite-id name="id" class="PersonKey"> <!-- 加上复合主键类的类名 --> <key-property name="name" column="NAME" type="string"/> <key-property name="phone" column="PHONE" type="string"/> </composite-id> <property name="age" column="AGE" type="int" /> </class> </hibernate-mapping>
相关文章推荐
- Hibernate学习_003_Hibernate中常用的主键生成策略总结(上)XML方式
- hibernate学习11之composite(复合)主键映射
- Hibernate 学习笔记( 三)-- 复合主键
- 【HIbernate框架学习】:Hibernate复合主键映射
- Hibernate学习总结:OneToOne单向主键关联
- Hibernate学习21 -- 关联映射11 -- 复合主键映射(composite-id)
- Hibernate学习_004_Hibernate中常用的主键生成策略总结(下)Annotation方式
- Hibernate映射详解(七)--复合主键映射与Component映射
- Java程序员从笨鸟到菜鸟之(六十)细谈Hibernate(十一)hibernate复合主键映射
- hibernate学习和各种关系总结
- Hibernate学习总结
- Hibernate 调用带有复合主键的stored procedure
- Hibernate复合主键的使用
- 学习struts2建bbs总结六:hibernate分页查询的问题--分页后程序定时无响应
- Hibernate主键生成策略总结
- Hibernate的学习之路九(主键的生成策略)
- Hibernate 使用复合主键
- 解决使用Hibernate QBC复合查询含有联合主键映射关系实体的问题
- Hibernate学习总结
- Hibernate复合主键映射