quchj 2011-05-02
3、组件类作为持久化类的对象标识符属性
这种情况主要是针对已经数据库表建模时,由于数据库表中采用联合自然主键(复合自然主键),为了完成这种映射需求,Hibernate使用组件类把它映射成持久化类的对象标识符。
Hibernate要求,作为复合对象标识符类的UserOid类必须实现Java.io.Serializable,且要用作为复合标识符的属性重写hashCode()方法和equlas()方法。User.java
package com.zxf.domain; /** 用户持久化类(实体类) */ public class User { private UserOid id; //对象标识符(OID) private String description; //描述 public User(){} //无参数的构造方法 //以下省略所有属性的getters和setters方法... }
package com.zxf.domain; /** 用户持久化类(实体类) */ public class User { private UserOid id; //对象标识符(OID) private String description; //描述 public User(){} //无参数的构造方法 //以下省略所有属性的getters和setters方法... }
UserOid .java
package com.zxf.domain; /** 用户类的对象标识符类 * 必须实现Serializable和重写hashCode()和equals()方法 */ public class UserOid implements java.io.Serializable { private static final long serialVersionUID = -9079299317576122433L; private String firstName; //名 private String lastName; //姓 public UserOid(){} //无参数的构造方法 //用fristName、lastName重写hashCode方法 public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result; } //用fristName、lastName重写equals方法 public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserOid other = (UserOid) obj; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; return true; } //以下省略所有属性的getters和setters方法... }
package com.zxf.domain; /** 用户类的对象标识符类 * 必须实现Serializable和重写hashCode()和equals()方法 */ public class UserOid implements java.io.Serializable { private static final long serialVersionUID = -9079299317576122433L; private String firstName; //名 private String lastName; //姓 public UserOid(){} //无参数的构造方法 //用fristName、lastName重写hashCode方法 public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result; } //用fristName、lastName重写equals方法 public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserOid other = (UserOid) obj; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; return true; } //以下省略所有属性的getters和setters方法... }
User.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> <!-- 映射持久化类 --> <class name="com.zxf.domain.User" table="user"> <!-- composite-id元素映射复合对象标识符 name属性:指定属性名 class属性:指定对应的此属性的类类型 --> <composite-id name="id" class="com.zxf.domain.UserOid"> <!-- key-property元素映射组件类的的每个属性 --> <key-property name="firstName" column="first_name"/> <key-property name="lastName" column="last_name"/> </composite-id> <!-- 用property映射每个普通属性 --> <property name="description"/> </class> </hibernate-mapping>
hibernate.cfg.xml