hibernate复合主键及关联的实现

如果你不得不面对遗留系统老式的数据库复合主键,无法享受逻辑主键(代理主键)带来的幸福生活,那么使用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);
	}
 

你可能感兴趣的:(sql,.net,Hibernate,生活)