04 | * 需要持久化的enum类,都需要实现的接口 |
09 | public interface PersistEnum<E extends Enum<?>> { |
16 | String getPersistedValue(); |
21 | * @param persistedValue |
24 | E returnEnum(String persistedValue); |
31 | Map<String, E> getAllValueMap(); |
001 | import java.io.Serializable; |
002 | import java.lang.reflect.InvocationTargetException; |
003 | import java.lang.reflect.Method; |
004 | import java.sql.PreparedStatement; |
005 | import java.sql.ResultSet; |
006 | import java.sql.SQLException; |
007 | import java.sql.Types; |
008 | import java.util.Properties; |
010 | import org.hibernate.HibernateException; |
011 | import org.hibernate.usertype.ParameterizedType; |
012 | import org.hibernate.usertype.UserType; |
013 | import org.springframework.util.ObjectUtils; |
016 | * 用户持久化枚举类型的用户自定义hibernate映射类型 |
019 | * 使用此类型来进行映射的枚举类,必须实现{@link com.lwei.common.PersistEnum} 接口 |
023 | public class TopEnumType implements UserType, |
026 | private Method returnEnum; |
028 | private Method getPersistedValue; |
030 | private Class<Enum<?>> enumClass; |
032 | private Object enumObject; |
035 | * This method uses the parameter values passed during enum mapping |
036 | * definition and sets corresponding properties defined |
038 | @SuppressWarnings ( "unchecked" ) |
039 | public void setParameterValues(Properties parameters) { |
040 | if (parameters != null ) { |
042 | enumClass = (Class<Enum<?>>) Class.forName(parameters.get( "enumClass" ).toString()); |
043 | enumObject = enumClass.getEnumConstants()[ 0 ]; |
044 | getPersistedValue = enumClass.getMethod( "getPersistedValue" ); |
045 | returnEnum = enumClass.getMethod( "returnEnum" , |
046 | new Class[] { String. class }); |
047 | } catch (SecurityException e) { |
049 | } catch (NoSuchMethodException e) { |
051 | } catch (ClassNotFoundException e) { |
058 | * This method maps the database mapping |
060 | public int [] sqlTypes() { |
061 | return new int [] { Types.VARCHAR }; |
065 | * This method maps the class for which user type is created |
067 | public Class<?> returnedClass() { |
071 | public boolean equals(Object x, Object y) throws HibernateException { |
072 | return ObjectUtils.nullSafeEquals(x, y); |
076 | * Fetch the hash code |
078 | public int hashCode(Object x) throws HibernateException { |
083 | * Recreate the enum from the resultset |
085 | public Object nullSafeGet(ResultSet rs, String[] names, Object owner) |
086 | throws HibernateException, SQLException { |
088 | String value = rs.getString(names[ 0 ]); |
089 | Object returnVal = null ; |
095 | returnVal = returnEnum |
096 | .invoke(enumObject, new Object[] { value }); |
097 | } catch (IllegalArgumentException e) { |
099 | } catch (IllegalAccessException e) { |
101 | } catch (InvocationTargetException e) { |
109 | * Fetch the data from enum and set it in prepared statement |
111 | public void nullSafeSet(PreparedStatement st, Object value, int index) |
112 | throws HibernateException, SQLException { |
113 | String prepStmtVal = null ; |
116 | st.setObject(index, null ); |
119 | prepStmtVal = getPersistedValue.invoke(value).toString(); |
120 | st.setString(index, prepStmtVal); |
121 | } catch (IllegalArgumentException e) { |
123 | } catch (IllegalAccessException e) { |
125 | } catch (InvocationTargetException e) { |
134 | public Object deepCopy(Object value) throws HibernateException { |
138 | public boolean isMutable() { |
142 | public Serializable disassemble(Object value) throws HibernateException { |
143 | Object deepCopy = deepCopy(value); |
145 | if (!(deepCopy instanceof Serializable)) |
146 | return (Serializable) deepCopy; |
151 | public Object assemble(Serializable cached, Object owner) |
152 | throws HibernateException { |
153 | return deepCopy(cached); |
156 | public Object replace(Object original, Object target, Object owner) |
157 | throws HibernateException { |
158 | return deepCopy(original); |
02 | import java.util.TreeMap; |
04 | import com.lwei.common.TopEnum; |
06 | public enum OrganType implements PersistEnum<OrganType>{ |
08 | ORGANTYPE_DEPARTMENT( "D" ), // 部门 |
09 | ORGANTYPE_ORGANMANAGER( "M" ), // 管理机构 |
10 | ORGANTYPE_NONE( "N" ); //普通机构 无特殊含意的机构,类似企业中的总公司和分支机构,只有上下级关系。 |
12 | private static final Map<String, OrganType> map = new TreeMap<String, OrganType>(); |
15 | map.put(ORGANTYPE_DEPARTMENT.getOrgType(), ORGANTYPE_DEPARTMENT); |
16 | map.put(ORGANTYPE_ORGANMANAGER.getOrgType(), ORGANTYPE_ORGANMANAGER); |
17 | map.put(ORGANTYPE_NONE.getOrgType(), ORGANTYPE_NONE); |
21 | private String orgType ; |
24 | private OrganType(String _orgType) { |
25 | this . orgType = _orgType; |
28 | public String getOrgType() { |
33 | public String getPersistedValue() { |
38 | public OrganType returnEnum(String persistedValue) { |
39 | return map.get(persistedValue); |
43 | public Map<String, OrganType> getAllValueMap() { |
1 | @Column (name = "organType" , nullable = false , length = 1 ) |
2 | @Type (type= "com.lwei.persistence.TopEnumType" , |
3 | parameters={ @Parameter (name= "enumClass" ,value= "com.lwei.org.domain.entities.OrganType" )}) |
4 | private OrganType organType; |
|