사용자 도구

사이트 도구


java:jpa:enum

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
java:jpa:enum [2014/10/01 18:21]
kwon37xi
java:jpa:enum [2014/10/01 21:01]
kwon37xi
줄 2: 줄 2:
   * [[http://appfuse.org/display/APF/Java+5+Enums+Persistence+with+Hibernate|Java 5 Enums Persistence with Hibernate]]   * [[http://appfuse.org/display/APF/Java+5+Enums+Persistence+with+Hibernate|Java 5 Enums Persistence with Hibernate]]
   * JPA 2.0에서는 ENUM값을 자유롭게 변경해서 넣을 때 문제가 있다. JPA 2.1에서는 Converter가 있어서 관계 없음.   * JPA 2.0에서는 ENUM값을 자유롭게 변경해서 넣을 때 문제가 있다. JPA 2.1에서는 Converter가 있어서 관계 없음.
 +
 +====== Hibernate UserType ======
   * [[https://developer.jboss.org/wiki/Java5EnumUserType|Java 5 EnumUserType]]   * [[https://developer.jboss.org/wiki/Java5EnumUserType|Java 5 EnumUserType]]
   * [[http://stackoverflow.com/questions/4744179/java-lang-verifyerror-on-hibernate-specific-usertype/6231710#6231710|java.lang.verifyError on hibernate specific usertype for Hibernate 4 - Stack Overflow]]   * [[http://stackoverflow.com/questions/4744179/java-lang-verifyerror-on-hibernate-specific-usertype/6231710#6231710|java.lang.verifyError on hibernate specific usertype for Hibernate 4 - Stack Overflow]]
-====== Hibernate UserType ====== +  * 위 두 링크에 있는 UserType을 Hibernate 4 용으로 변환한 것. 
-for Hibernate 4+
 <code java> <code java>
-package org.appfuse.tutorial.commons.hibernate; 
-  
-import java.io.Serializable; 
-import java.lang.reflect.Method; 
-import java.sql.PreparedStatement; 
-import java.sql.ResultSet; 
-import java.sql.SQLException; 
-import java.util.Properties; 
-  
-import org.hibernate.HibernateException; 
-import org.hibernate.type.NullableType; 
-import org.hibernate.type.TypeFactory; 
-import org.hibernate.usertype.ParameterizedType; 
-import org.hibernate.usertype.UserType; 
-  
-  
 public class GenericEnumUserType implements UserType, ParameterizedType { public class GenericEnumUserType implements UserType, ParameterizedType {
-    private static final String DEFAULT_IDENTIFIER_METHOD_NAME = "name"; + 
-    private static final String DEFAULT_VALUE_OF_METHOD_NAME = "valueOf"; + /** 대상 enum 클래스를 문자열로 지정한다 */ 
-  + public static final String PARAM_ENUM_CLASS = "enumClass"; 
-    private Class<? extends Enum> enumClass; + 
-    private Class<?> identifierType; + /** Enum을 실제 저장할 문자열로 바꿔주는 메소드. 이 메소드의 리턴타입은 PARAM_VALUE_OF_METHOD로 지정된 메소드의 파라미터와 같은 타입이어야 한다. */ 
-    private Method identifierMethod; + public static final String PARAM_IDENTIFIER_METHOD = "identifierMethod"; 
-    private Method valueOfMethod; + private static final String DEFAULT_IDENTIFIER_METHOD_NAME = "name"; 
-    private NullableType type; + 
-    private int[] sqlTypes; + /** 저장된 데이터로부터 Enum을 리턴해주는 메소드. 이 메소드의 파라미터 타입은 PARAM_IDENTIFIER_METHOD로 지정된 메소드의 리턴 타입과 같아야 한다. */ 
-  + public static final String PARAM_VALUE_OF_METHOD = "valueOfMethod"; 
-    public void setParameterValues(Properties parameters) { + private static final String DEFAULT_VALUE_OF_METHOD_NAME = "valueOf"; 
-        String enumClassName = parameters.getProperty("enumClass"); + 
-        try { + private Class<? extends Enum> enumClass; 
-            enumClass = Class.forName(enumClassName).asSubclass(Enum.class); + private Class<?> identifierType; 
-        } catch (ClassNotFoundException cfne) { + private Method identifierMethod; 
-            throw new HibernateException("Enum class not found", cfne); + private Method valueOfMethod; 
-        + private AbstractSingleColumnStandardBasicType type; 
-  + private int[] sqlTypes; 
-        String identifierMethodName = parameters.getProperty("identifierMethod", DEFAULT_IDENTIFIER_METHOD_NAME); + 
-  + @Override 
-        try { + public void setParameterValues(Properties parameters) { 
-            identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]); + String enumClassName = parameters.getProperty(PARAM_ENUM_CLASS); 
-            identifierType = identifierMethod.getReturnType(); + 
-        } catch (Exception e) { + try { 
-            throw new HibernateException("Failed to obtain identifier method", e); + enumClass = Class.forName(enumClassName).asSubclass(Enum.class); 
-        + } catch (ClassNotFoundException exception) { 
-  + throw new HibernateException("Enum class not found", exception); 
-        type = (NullableTypeTypeFactory.basic(identifierType.getName()); +
-  + 
-        if (type == null) + String identifierMethodName = parameters.getProperty(PARAM_IDENTIFIER_METHOD, DEFAULT_IDENTIFIER_METHOD_NAME); 
-            throw new HibernateException("Unsupported identifier type " + identifierType.getName()); + 
-  + try { 
-        sqlTypes = new int[] { type.sqlType() }; + identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]); 
-  + identifierType = identifierMethod.getReturnType(); 
-        String valueOfMethodName = parameters.getProperty("valueOfMethod", DEFAULT_VALUE_OF_METHOD_NAME); + } catch (Exception exception) { 
-  + throw new HibernateException("Failed to optain identifier method", exception); 
-        try { +
-            valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType }); + 
-        } catch (Exception e) { + TypeResolver tr = new TypeResolver(); 
-            throw new HibernateException("Failed to obtain valueOf method", e); + type = (AbstractSingleColumnStandardBasicTypetr.basic(identifierType.getName()); 
-        + 
-    + if (type == null) { 
-  + throw new HibernateException("Unsupported identifier type " + identifierType.getName()); 
-    public Class returnedClass() { + } 
-        return enumClass; + sqlTypes = new int[] { type.sqlType() }; 
-    + 
-  + String valueOfMethodName = parameters.getProperty(PARAM_VALUE_OF_METHOD, DEFAULT_VALUE_OF_METHOD_NAME); 
-    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {  + 
-        Object identifier = type.get(rs, names[0]); + try { 
-        if (identifier == null) { + valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType }); 
-            return null; + } catch (Exception exception) { 
-        } + throw new HibernateException("Failed to optain valueOf method", 
-          + exception); 
-        try { +
-            return valueOfMethod.invoke(enumClass, new Object[] { identifier }); +
-        } catch (Exception e) { + 
-            throw new HibernateException("Exception while invoking valueOf method '"valueOfMethod.getName() + "' of " + + @Override 
-                    "enumeration class '" + enumClass + "'", e); + public Class returnedClass() { 
-        + return enumClass; 
-    +
-  + 
-    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { + @Override 
-        try { + public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { 
-            if (value == null) { + Object identifier = type.get(rs, names[0], session); 
-                st.setNull(index, type.sqlType()); + try { 
-            } else { + return valueOfMethod.invoke(enumClass, new Object[] { identifier }); 
-                Object identifier = identifierMethod.invoke(value, new Object[0]); + } catch (Exception exception) { 
-                type.set(st, identifier, index); + throw new HibernateException("Exception while invoking valueOfMethod of enumeration class", 
-            } + exception); 
-        } catch (Exception e) { +
-            throw new HibernateException("Exception while invoking identifierMethod '" + identifierMethod.getName() + "' of " + +
-                    "enumeration class '" + enumClass + "'", e); + 
-        + @Override 
-    + public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { 
-  + try { 
-    public int[] sqlTypes() { + Object identifier = value != null identifierMethod.invoke(value, new Object[0]) : null
-        return sqlTypes; + st.setObject(index, identifier); 
-    + } catch (Exception exception) { 
-  + throw new HibernateException("Exception while invoking identifierMethod of enumeration class", 
-    public Object assemble(Serializable cached, Object owner) throws HibernateException { + exception); 
-        return cached; +
-    +
-  + 
-    public Object deepCopy(Object value) throws HibernateException { + @Override 
-        return value; + public int[] sqlTypes() { 
-    + return sqlTypes; 
-  +
-    public Serializable disassemble(Object value) throws HibernateException { + 
-        return (Serializable) value; + @Override 
-    + public Object assemble(Serializable cached, Object owner) throws HibernateException { 
-  + return cached; 
-    public boolean equals(Object x, Object y) throws HibernateException { +
-        return x == y; + 
-    + @Override 
-  + public Object deepCopy(Object value) throws HibernateException { 
-    public int hashCode(Object x) throws HibernateException { + return value; 
-        return x.hashCode(); +
-    + 
-  + @Override 
-    public boolean isMutable() { + public Serializable disassemble(Object value) throws HibernateException { 
-        return false; + return (Serializable) value; 
-    +
-  + 
-    public Object replace(Object original, Object target, Object owner) throws HibernateException { + @Override 
-        return original; + public boolean equals(Object x, Object y) throws HibernateException { 
-    }+ return x == y; 
 +
 + 
 + @Override 
 + public int hashCode(Object x) throws HibernateException { 
 + return x.hashCode(); 
 +
 + 
 + public boolean isMutable() { 
 + return false; 
 +
 + 
 + public Object replace(Object original, Object target, Object owner) throws HibernateException { 
 + return original; 
 + }
 } }
 </code> </code>
java/jpa/enum.txt · 마지막으로 수정됨: 2014/10/02 16:40 저자 kwon37xi