hibernate 自定义枚举类型映射
来源:互联网 发布:河北js防水厂家 编辑:程序博客网 时间:2024/06/06 04:59
自定义类型MyEnumType
package com.binarysource.hinernate;import java.io.Serializable;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Types;import java.util.Properties;import org.hibernate.HibernateException;import org.hibernate.engine.spi.SessionImplementor;import org.hibernate.usertype.ParameterizedType;import org.hibernate.usertype.UserType;public class MyEnumType implements UserType, ParameterizedType{ private Field typeField; private Class<Enum<?>> enumClass; private Method method;@SuppressWarnings("unchecked")@Overridepublic void setParameterValues(Properties parameters) {if (parameters != null) { try { enumClass = (Class<Enum<?>>) Class.forName(parameters.get("enumClass").toString()); typeField=enumClass.getDeclaredField("type"); typeField.setAccessible(true); try {method=enumClass.getDeclaredMethod("getByType", String.class);} catch (NoSuchMethodException e) {} } catch (SecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) {e.printStackTrace(); } }}@Overridepublic int[] sqlTypes() {return new int[] { Types.VARCHAR };}@Overridepublic Class<?> returnedClass() {return enumClass;}@Overridepublic boolean equals(Object x, Object y) throws HibernateException { return (x!=null && y!=null) ? x.equals(y) : false;}@Overridepublic int hashCode(Object x) throws HibernateException {return x.hashCode();} @Overridepublic Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)throws HibernateException, SQLException { String value = rs.getString(names[0]); Object returnVal = null; if (value == null) return null; else { try { if(method!=null){ return method.invoke(null, value); } Enum<?>[] consts=enumClass.getEnumConstants(); for (Enum<?> enum1 : consts) { String type=(String)typeField.get(enum1); if(value.equals(type)){ return enum1; } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return returnVal;}@Overridepublic void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)throws HibernateException, SQLException { String prepStmtVal = null; if (value == null) { st.setObject(index, null); } else { try { prepStmtVal = typeField.get(value).toString(); st.setString(index, prepStmtVal); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }} /** * Deep copy method */ public Object deepCopy(Object value) throws HibernateException { return value; } public boolean isMutable() { return false; } public Serializable disassemble(Object value) throws HibernateException { Object deepCopy = deepCopy(value); if (!(deepCopy instanceof Serializable)) return (Serializable) deepCopy; return null; } public Object assemble(Serializable cached, Object owner) throws HibernateException { return deepCopy(cached); } public Object replace(Object original, Object target, Object owner) throws HibernateException { return deepCopy(original); }}
定义枚举类型
package com.binarysource.hibernate.enumeration;public enum SexType {Male("0","男"),Female("1","女");private String type;private String name;public static String getNameByType(String type){SexType[] modes = SexType.values();for (SexType mode : modes) {if(mode.type.equals(type)){return mode.name;}}return null;}public static SexType getSexTypeByName(String name){SexType[] modes = SexType.values();for (SexType mode : modes) {if(mode.name.equals(name)){return mode;}}return null;}private SexType(String type,String name){this.type = type;this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
entity中使用枚举
private SexType sexType; @Type(type="com.binarysource.hibernate.MyEnumType",parameters={@Parameter(name="enumClass",value="com.binarysource.hibernate.SexType")}) public SexType getSexType() { return sexType; } public void setSexType(SexType sexType) { this.sexType = sexType; }
0 0
- hibernate 自定义枚举类型映射
- Hibernate中映射枚举类型
- Hibernate如何映射枚举类型
- Hibernate中映射枚举类型
- * hibernate3 自定义枚举映射类型
- 【Hibernate】Hibernate如何映射枚举类型
- 使用Hibernate的XML配置来映射枚举类型,存储自定义类型数据
- Hibernate枚举类型数据库映射方案
- hibernate映射时间和枚举类型
- Hibernate如何映射枚举类型num
- Hibernate 映射枚举(Enum) 类型的属性
- Hibernate将Enum枚举类型映射为Int类型
- Hibernate将Enum枚举类型映射为Int类型
- Hibernate映射枚举类型和实体类型的区别
- hibernate 4 映射组件属性 自定义类型
- Hibernate中枚举Enum类型的映射策略
- Hibernate中枚举Enum类型的映射策略
- Hibernate使用自定义类型UserType映射Oracle的Date类型
- OC原生代码/webview与js/网页交互的最好方式
- MPAndroidChart 之LineChart(1)
- 啊啊啊
- python基础知识-列表
- linux下postgresql的备份与恢复
- hibernate 自定义枚举类型映射
- 在 Java 中使用 JSON
- Android 身份证扫描
- 不使用USERADD,手动添加用户
- eclipse下svn常见问题
- beanstalkd消息队列在生产环境的应用
- Android进程保活的一般套路
- 基于潘文明至简设计法实现的PWM调制verilog
- MySQL数据库TDSQL架构分析及采用策略扩容流程