hibernate复合主键及关联的实现

键字: 复合主键 关联

如果你不得不面对遗留系统老式的数据库复合主键,无法享受逻辑主键(代理主键)带来的幸福生活,那么使用CompositeUserType来处理复合主键是个不错的选择.废话少说,看看如何实现:


Java代码 复制代码
  1. /**  
  2.  * $Revision: 1.0 $  
  3.  * Created: 2008-1-11  
  4.  * $Date: 2008-1-11 $  
  5.  *   
  6.  * Author: Keven Chen  
  7.  */  
  8. package com.comwave.ww_oa.webui.org;   
  9.   
  10. import java.io.Serializable;   
  11.   
  12. /**  
  13.  * @author Keven Chen  
  14.  * @version $Revision 1.0 $  
  15.  *  
  16.  */  
  17. public class UserPositionId implements Serializable {   
  18.     private String userId;   
  19.   
  20.     private String posId;   
  21.        
  22.     public UserPositionId(){}   
  23.        
  24.     public UserPositionId(String userId, String posId) {   
  25.         this.userId = userId;   
  26.         this.posId = posId;   
  27.     }   
  28.   
  29.     public String getPosId() {   
  30.         return posId;   
  31.     }   
  32.   
  33.     public void setPosId(String posId) {   
  34.         this.posId = posId;   
  35.     }   
  36.   
  37.     public String getUserId() {   
  38.         return userId;   
  39.     }   
  40.   
  41.     public void setUserId(String userId) {   
  42.         this.userId = userId;   
  43.     }   
  44.   
  45.     public int hashCode() {   
  46.         final int PRIME = 31;   
  47.         int result = 1;   
  48.         result = PRIME * result + ((posId == null) ? 0 : posId.hashCode());   
  49.         result = PRIME * result + ((userId == null) ? 0 : userId.hashCode());   
  50.         return result;   
  51.     }   
  52.   
  53.     public boolean equals(Object obj) {   
  54.         if (this == obj)   
  55.             return true;   
  56.         if (obj == null)   
  57.             return false;   
  58.         if (getClass() != obj.getClass())   
  59.             return false;   
  60.         final UserPositionId other = (UserPositionId) obj;   
  61.         if (posId == null) {   
  62.             if (other.posId != null)   
  63.                 return false;   
  64.         } else if (!posId.equals(other.posId))   
  65.             return false;   
  66.         if (userId == null) {   
  67.             if (other.userId != null)   
  68.                 return false;   
  69.         } else if (!userId.equals(other.userId))   
  70.             return false;   
  71.         return true;   
  72.     }   
  73.        
  74. }  
/**
 * $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;
	}
	
}
 

 

 

Java代码 复制代码
  1. /**  
  2.  * $Revision: 1.0 $  
  3.  * Created: 2008-1-11  
  4.  * $Date: 2008-1-11 $  
  5.  *   
  6.  * Author: Keven Chen  
  7.  */  
  8. package com.comwave.ww_oa.webui.org;   
  9.   
  10. import java.io.Serializable;   
  11. import java.sql.PreparedStatement;   
  12. import java.sql.ResultSet;   
  13. import java.sql.SQLException;   
  14.   
  15. import org.hibernate.Hibernate;   
  16. import org.hibernate.HibernateException;   
  17. import org.hibernate.engine.SessionImplementor;   
  18. import org.hibernate.type.Type;   
  19. import org.hibernate.usertype.CompositeUserType;   
  20.   
  21. /**  
  22.  * @author Keven Chen  
  23.  * @version $Revision 1.0 $  
  24.  *   
  25.  */  
  26. public class UserPositionIdUserType implements CompositeUserType {   
  27.   
  28.     public Object assemble(Serializable cached, SessionImplementor session, Object owner)   
  29.             throws HibernateException {   
  30.         return deepCopy(cached);   
  31.     }   
  32.   
  33.     public Object deepCopy(Object value) throws HibernateException {   
  34.         UserPositionId id = (UserPositionId) value;   
  35.         return new UserPositionId(id.getUserId(),id.getPosId());   
  36.     }   
  37.   
  38.     public Serializable disassemble(Object value, SessionImplementor session)   
  39.             throws HibernateException {   
  40.         return (Serializable) deepCopy(value);   
  41.     }   
  42.   
  43.   
  44.     public boolean equals(Object x, Object y) throws HibernateException {   
  45.         if (x==y) return true;   
  46.         if (x==null || y==nullreturn false;   
  47.         return x.equals(y);   
  48.     }   
  49.   
  50.     public String[] getPropertyNames() {   
  51.         return new String[] { "userId""posId" };   
  52.     }   
  53.   
  54.     public Type[] getPropertyTypes() {   
  55.         return new Type[] { Hibernate.STRING, Hibernate.STRING };   
  56.     }   
  57.   
  58.     public Object getPropertyValue(Object component, int property) throws HibernateException {   
  59.         UserPositionId id = (UserPositionId) component;   
  60.         return property == 0 ? id.getUserId() : id.getPosId();   
  61.     }   
  62.   
  63.     public void setPropertyValue(Object component, int property, Object value)   
  64.             throws HibernateException {   
  65.         UserPositionId id = (UserPositionId) component;   
  66.         if (property == 0) {   
  67.             id.setUserId((String) value);   
  68.         } else {   
  69.             id.setPosId((String) value);   
  70.         }   
  71.   
  72.     }   
  73.   
  74.     public int hashCode(Object x) throws HibernateException {   
  75.         return x.hashCode();   
  76.     }   
  77.   
  78.     public boolean isMutable() {   
  79.         return true;   
  80.     }   
  81.   
  82.     public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)   
  83.             throws HibernateException, SQLException {   
  84.         String userId = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);   
  85.         String posId = (String) Hibernate.STRING.nullSafeGet(rs, names[1]);   
  86.         return new UserPositionId(userId,posId);   
  87.     }   
  88.   
  89.     public void nullSafeSet(PreparedStatement st, Object value, int index,   
  90.             SessionImplementor session) throws HibernateException, SQLException {   
  91.         UserPositionId id = (UserPositionId) value;   
  92.         String userId = (id == null) ? null : id.getUserId();   
  93.         String posId = (id == null) ? null :id.getPosId();   
  94.         Hibernate.STRING.nullSafeSet(st,userId, index);   
  95.         Hibernate.STRING.nullSafeSet(st,posId, index+1);   
  96.     }   
  97.   
  98.     public Object replace(Object original, Object target, SessionImplementor session, Object owner)   
  99.             throws HibernateException {   
  100.         return deepCopy(original); //TODO: improve   
  101.     }   
  102.   
  103.     public Class returnedClass() {   
  104.         return UserPositionId.class;   
  105.     }   
  106. }  
/**
 * $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;
	}
}

 

Java代码 复制代码
  1. /**  
  2.  * $Revision: 1.0 $  
  3.  * Created: 2008-1-10  
  4.  * $Date: 2008-1-10 $  
  5.  *   
  6.  * Author: Keven Chen  
  7.  */  
  8. package com.comwave.ww_oa.webui.org;   
  9.   
  10. import java.util.ArrayList;   
  11. import java.util.List;   
  12.   
  13. /**  
  14.  * @author Keven Chen  
  15.  * @version $Revision 1.0 $  
  16.  *   
  17.  */  
  18. public class UserPosition {   
  19.        
  20.     private UserPositionId id = new UserPositionId();   
  21.        
  22.     private User user;   
  23.   
  24.     private Position postion;   
  25.   
  26.     private UserPosition director;   
  27.   
  28.     private boolean dept_director;   
  29.   
  30.     private boolean unit_director;   
  31.   
  32.     private boolean top_unit_director;   
  33.        
  34.     private List underling = new ArrayList();   
  35.   
  36.     public List getUnderling() {   
  37.         return underling;   
  38.     }   
  39.   
  40.     public void setUnderling(List underling) {   
  41.         this.underling = underling;   
  42.     }   
  43.   
  44.     public boolean isDept_director() {   
  45.         return dept_director;   
  46.     }   
  47.   
  48.     public void setDept_director(boolean dept_director) {   
  49.         this.dept_director = dept_director;   
  50.     }   
  51.   
  52.     public Position getPostion() {   
  53.         return postion;   
  54.     }   
  55.   
  56.     public void setPostion(Position postion) {   
  57.         this.postion = postion;   
  58.     }   
  59.   
  60.     public boolean isTop_unit_director() {   
  61.         return top_unit_director;   
  62.     }   
  63.   
  64.     public void setTop_unit_director(boolean top_unit_director) {   
  65.         this.top_unit_director = top_unit_director;   
  66.     }   
  67.   
  68.     public boolean isUnit_director() {   
  69.         return unit_director;   
  70.     }   
  71.   
  72.     public void setUnit_director(boolean unit_director) {   
  73.         this.unit_director = unit_director;   
  74.     }   
  75.   
  76.     public User getUser() {   
  77.         return user;   
  78.     }   
  79.   
  80.     public void setUser(User user) {   
  81.         this.user = user;   
  82.     }   
  83.   
  84.     public UserPosition getDirector() {   
  85.         return director;   
  86.     }   
  87.   
  88.     public void setDirector(UserPosition director) {   
  89.         this.director = director;   
  90.     }   
  91.   
  92.     public UserPositionId getId() {   
  93.         return id;   
  94.     }   
  95.   
  96.     public void setId(UserPositionId id) {   
  97.         this.id = id;   
  98.     }   
  99.   
  100.     public int hashCode() {   
  101.         final int PRIME = 31;   
  102.         int result = 1;   
  103.         result = PRIME * result + ((id == null) ? 0 : id.hashCode());   
  104.         return result;   
  105.     }   
  106.   
  107.     public boolean equals(Object obj) {   
  108.         if (this == obj)   
  109.             return true;   
  110.         if (obj == null)   
  111.             return false;   
  112.         if (getClass() != obj.getClass())   
  113.             return false;   
  114.         final UserPosition other = (UserPosition) obj;   
  115.         if (id == null) {   
  116.             if (other.id != null)   
  117.                 return false;   
  118.         } else if (!id.equals(other.id))   
  119.             return false;   
  120.         return true;   
  121.     }   
  122.   
  123. }  
/**
 * $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代码 复制代码
  1. <?xml version="1.0"  encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"   
  3.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <hibernate-mapping package="com.comwave.ww_oa.webui.org">  
  5.     <class name="UserPosition" table="user_positions">  
  6.         <id name="id" type="com.comwave.ww_oa.webui.org.UserPositionIdUserType" unsaved-value="any">  
  7.             <column name="userId"/>  
  8.             <column name="posId"/>  
  9.         </id>  
  10.         <property name="dept_director" type="boolean"/>  
  11.         <property name="unit_director" type="boolean"/>  
  12.         <property name="top_unit_director" type="boolean"/>  
  13.         <bag name="underling" inverse="true">  
  14.             <key>  
  15.                 <column name="director_user_id"/>  
  16.                 <column name="director_pos_id"/>  
  17.             </key>  
  18.             <one-to-many class="UserPosition"/>  
  19.         </bag>  
  20.         <many-to-one name="director">  
  21.             <column name="director_user_id"/>  
  22.             <column name="director_pos_id"/>  
  23.         </many-to-one>  
  24.     </class>  
  25. </hibernate-mapping>  
 

使用:和正常的类相同的使用方式

 

Java代码 复制代码
  1. public UserPosition getPosition(String userId,String posId) {   
  2.         return getPosition(new UserPositionId(userId,posId));   
  3.     }   
  4.        
  5.     public UserPosition getPosition(UserPositionId id){   
  6.         return (UserPosition) getHibernateTemplate().get(UserPosition.class,id);   
  7.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值