如果你不得不面对遗留系统老式的数据库复合主键,无法享受逻辑主键(代理主键)带来的幸福生活,那么使用CompositeUserType来处理复合主键是个不错的选择.废话少说,看看如何实现:
/**
* $Revision: 1.0 $
* Created: 2008-1-11
* $Date: 2008-1-11 $
*
* Author: Keven Chen
*/
package com.comwave.ww_oa.webui.org;
import java.io.Serializable;
/**
* @author Keven Chen
* @version $Revision 1.0 $
*
*/
public class UserPositionId implements Serializable {
private String userId;
private String posId;
public UserPositionId(){}
public UserPositionId(String userId, String posId) {
this.userId = userId;
this.posId = posId;
}
public String getPosId() {
return posId;
}
public void setPosId(String posId) {
this.posId = posId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((posId == null) ? 0 : posId.hashCode());
result = PRIME * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final UserPositionId other = (UserPositionId) obj;
if (posId == null) {
if (other.posId != null)
return false;
} else if (!posId.equals(other.posId))
return false;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}
}
/**
* $Revision: 1.0 $
* Created: 2008-1-11
* $Date: 2008-1-11 $
*
* Author: Keven Chen
*/
package com.comwave.ww_oa.webui.org;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
/**
* @author Keven Chen
* @version $Revision 1.0 $
*
*/
public class UserPositionIdUserType implements CompositeUserType {
public Object assemble(Serializable cached, SessionImplementor session, Object owner)
throws HibernateException {
return deepCopy(cached);
}
public Object deepCopy(Object value) throws HibernateException {
UserPositionId id = (UserPositionId) value;
return new UserPositionId(id.getUserId(),id.getPosId());
}
public Serializable disassemble(Object value, SessionImplementor session)
throws HibernateException {
return (Serializable) deepCopy(value);
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x==y) return true;
if (x==null || y==null) return false;
return x.equals(y);
}
public String[] getPropertyNames() {
return new String[] { "userId", "posId" };
}
public Type[] getPropertyTypes() {
return new Type[] { Hibernate.STRING, Hibernate.STRING };
}
public Object getPropertyValue(Object component, int property) throws HibernateException {
UserPositionId id = (UserPositionId) component;
return property == 0 ? id.getUserId() : id.getPosId();
}
public void setPropertyValue(Object component, int property, Object value)
throws HibernateException {
UserPositionId id = (UserPositionId) component;
if (property == 0) {
id.setUserId((String) value);
} else {
id.setPosId((String) value);
}
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean isMutable() {
return true;
}
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
String userId = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
String posId = (String) Hibernate.STRING.nullSafeGet(rs, names[1]);
return new UserPositionId(userId,posId);
}
public void nullSafeSet(PreparedStatement st, Object value, int index,
SessionImplementor session) throws HibernateException, SQLException {
UserPositionId id = (UserPositionId) value;
String userId = (id == null) ? null : id.getUserId();
String posId = (id == null) ? null :id.getPosId();
Hibernate.STRING.nullSafeSet(st,userId, index);
Hibernate.STRING.nullSafeSet(st,posId, index+1);
}
public Object replace(Object original, Object target, SessionImplementor session, Object owner)
throws HibernateException {
return deepCopy(original); //TODO: improve
}
public Class returnedClass() {
return UserPositionId.class;
}
}
/**
* $Revision: 1.0 $
* Created: 2008-1-10
* $Date: 2008-1-10 $
*
* Author: Keven Chen
*/
package com.comwave.ww_oa.webui.org;
import java.util.ArrayList;
import java.util.List;
/**
* @author Keven Chen
* @version $Revision 1.0 $
*
*/
public class UserPosition {
private UserPositionId id = new UserPositionId();
private User user;
private Position postion;
private UserPosition director;
private boolean dept_director;
private boolean unit_director;
private boolean top_unit_director;
private List underling = new ArrayList();
public List getUnderling() {
return underling;
}
public void setUnderling(List underling) {
this.underling = underling;
}
public boolean isDept_director() {
return dept_director;
}
public void setDept_director(boolean dept_director) {
this.dept_director = dept_director;
}
public Position getPostion() {
return postion;
}
public void setPostion(Position postion) {
this.postion = postion;
}
public boolean isTop_unit_director() {
return top_unit_director;
}
public void setTop_unit_director(boolean top_unit_director) {
this.top_unit_director = top_unit_director;
}
public boolean isUnit_director() {
return unit_director;
}
public void setUnit_director(boolean unit_director) {
this.unit_director = unit_director;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserPosition getDirector() {
return director;
}
public void setDirector(UserPosition director) {
this.director = director;
}
public UserPositionId getId() {
return id;
}
public void setId(UserPositionId id) {
this.id = id;
}
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((id == null) ? 0 : id.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final UserPosition other = (UserPosition) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.comwave.ww_oa.webui.org">
<class name="UserPosition" table="user_positions">
<id name="id" type="com.comwave.ww_oa.webui.org.UserPositionIdUserType" unsaved-value="any">
<column name="userId"/>
<column name="posId"/>
</id>
<property name="dept_director" type="boolean"/>
<property name="unit_director" type="boolean"/>
<property name="top_unit_director" type="boolean"/>
<bag name="underling" inverse="true">
<key>
<column name="director_user_id"/>
<column name="director_pos_id"/>
</key>
<one-to-many class="UserPosition"/>
</bag>
<many-to-one name="director">
<column name="director_user_id"/>
<column name="director_pos_id"/>
</many-to-one>
</class>
</hibernate-mapping>
使用:和正常的类相同的使用方式
public UserPosition getPosition(String userId,String posId) {
return getPosition(new UserPositionId(userId,posId));
}
public UserPosition getPosition(UserPositionId id){
return (UserPosition) getHibernateTemplate().get(UserPosition.class,id);
}
分享到:
相关推荐
一个简单的复合主键的做关联类的例子
023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形结构 027 hibernate 查询语言(HQL) 028 hibernate 缓存(性能优化策略) 029 hibernate 抓取策略
hibernate一对一主键关联映射(单向关联Person---->IdCard) 8 hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一...
04 04Hibernate_Composite : 复合主键的使用,在开发中很少用到,一般良好的设计都会为一个表添加一个自动增长的主键标识列。其中重点配置方法和Hibernate中普遍采用的方法链编程的使用。还需注意可以将组合主键构建...
2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4...
2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. ...
21.5 利用Hibernate的版本控制来实现乐观锁 21.5.1 使用元素 21.5.2 使用元素 21.5.3 对游离对象进行版本检查 21.5.4 强制更新版本 21.6 实现乐观锁的其他方法 21.7 小结 21.8 思考题 第22章 管理...
18-Hibernate复合主键笔记 20-22Hibernate_容器映射技术笔记 23-26Hibernate数据关联技术笔记 27-32Hibernate数据关联技术_1vs1应用案例_笔记 33-37Hibernate1对N案例笔记 38-43Hibernate多对多案例笔记 J2EE框架_...
18-Hibernate复合主键笔记 20-22Hibernate_容器映射技术笔记 23-26Hibernate数据关联技术笔记 27-32Hibernate数据关联技术_1vs1应用案例_笔记 33-37Hibernate1对N案例笔记 38-43Hibernate多对多案例笔记 J2EE框架_...
2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4...
18-Hibernate复合主键笔记 20-22Hibernate_容器映射技术笔记 23-26Hibernate数据关联技术笔记 27-32Hibernate数据关联技术_1vs1应用案例_笔记 33-37Hibernate1对N案例笔记 38-43Hibernate多对多案例笔记 J2EE框架_...
18-Hibernate复合主键笔记.pdf 19-Hibernate实体层设计笔记.pdf 2-JSP+JDBC_真分页(基于Oracle数据库分页)笔记.pdf 20-22Hibernate_容器映射技术笔记.pdf 23-26Hibernate数据关联技术笔记.pdf 27-32Hibernate数据...
本章主要介绍关系数据库中的代理主键(不具有业务含义),接着介绍Hibernate提供的几种内置标识符生成器的用法及适用范围。 6.1 关系数据库按主键区分不同的记录 123 6.1.1 把主键定义为自动增长标识符类型 123 ...
21.5 利用Hibernate的版本控制来实现乐观锁 21.5.1 使用元素 21.5.2 使用元素 21.5.3 对游离对象进行版本检查 21.5.4 强制更新版本 21.6 实现乐观锁的其他方法 21.7 小结 21.8 思考题 第22章 管理...
21.5 利用Hibernate的版本控制来实现乐观锁 21.5.1 使用元素 21.5.2 使用元素 21.5.3 对游离对象进行版本检查 21.5.4 强制更新版本 21.6 实现乐观锁的其他方法 21.7 小结 21.8 思考题 第22章 管理...
21.5 利用Hibernate的版本控制来实现乐观锁 21.5.1 使用元素 21.5.2 使用元素 21.5.3 对游离对象进行版本检查 21.5.4 强制更新版本 21.6 实现乐观锁的其他方法 21.7 小结 21.8 思考题 第22章 管理...