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>
<?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
- <!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.MySQLInnoDBDialect
- </property>
- <!-- 数据库连接参数设置 -->
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property
- name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">123</property>
- <!--实际操作数据库时是否显示SQL -->
- <!--
- <property name="hibernate.show_sql">true</property>-->
- <property name="hibernate.format_sql">true</property>
- <!--将数据库schema的DDL导出到数据库 -->
- <property name="hibernate.hbm2ddl.auto">update</property>
- <!-- 以下定义实体类与数据库表的映像文件 -->
- <mapping resource="com/zxf/domain/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>