java - How can I store enums with fixed values in play -
i'm trying convert grails-project playframework. in grails can define id the id stored in database (see enhanced enum support in release notes). saw similar question, no acceptable solution. if change type crud-module problem, because information enum should shown lost.
so wonder if there exists nice solution play, based on hibernate. perhaps hacking jpaplugin?
[update 1] started try second solution @type
-annotation. unfortunately become broken hibernate 3.6 (which used play 1.2.2). typefactory.basic()
not available more. following documentation can't find work around.
[update 2] there solution hibernate 3.6.1, it's clumsy define type @ each usage of enum.
@type(type="hibernatehelper.genericenumusertype", parameters= { @parameter( name = "enumclass", value = "models.geschlecht"), }) public geschlecht geschlecht = geschlecht.weiblich;
not sure if it's work, 1 possible solution following:
wrote generic type mapper:
package hibernatehelper; 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.abstractsinglecolumnstandardbasictype; import org.hibernate.type.typeresolver; import org.hibernate.usertype.parameterizedtype; import org.hibernate.usertype.usertype; public class genericenumusertype implements usertype, parameterizedtype { private class <? extends enum> enumclass; private class <?> identifiertype; private method identifiermethod; private method valueofmethod; private static final string defaultidentifiermethodname = "getid"; private static final string defaultvalueofmethodname = "parseid"; private abstractsinglecolumnstandardbasictype type; private int[] sqltypes; @override public void setparametervalues(properties parameters) { string enumclassname = parameters.getproperty("enumclass"); try { enumclass = class.forname(enumclassname).assubclass(enum.class); } catch (classnotfoundexception exception) { throw new hibernateexception("enum class not found", exception); } string identifiermethodname = parameters.getproperty("identifiermethod", defaultidentifiermethodname); try { identifiermethod = enumclass.getmethod(identifiermethodname, new class[0]); identifiertype = identifiermethod.getreturntype(); } catch (exception exception) { throw new hibernateexception("failed optain identifier method", exception); } typeresolver tr = new typeresolver(); type = (abstractsinglecolumnstandardbasictype) tr.basic(identifiertype .getname()); if (type == null) { throw new hibernateexception("unsupported identifier type " + identifiertype.getname()); } sqltypes = new int[] {type.sqltype()}; string valueofmethodname = parameters.getproperty("valueofmethod", defaultvalueofmethodname); try { valueofmethod = enumclass.getmethod(valueofmethodname, new class[] {identifiertype}); } catch (exception exception) { throw new hibernateexception("failed optain valueof method", exception); } } @override public class returnedclass() { return enumclass; } @override public object nullsafeget(resultset rs, string[] names, object owner) throws hibernateexception, sqlexception { object identifier = type.get(rs, names[0]); if (identifier == null) { return null; } if (valueofmethod == null) { } try { return valueofmethod.invoke(enumclass, new object[] {identifier}); } catch (exception exception) { throw new hibernateexception( "exception while invoking valueofmethod of enumeration class: ", exception); } } public void nullsafeset(preparedstatement st, object value, int index) throws hibernateexception, sqlexception { try { object identifier = value != null ? identifiermethod.invoke(value, new object[0]) : null; st.setobject(index, identifier); } catch (exception exception) { throw new hibernateexception( "exception while invoking identifiermethod of enumeration class: ", exception); } } @override public int[] sqltypes() { return sqltypes; } @override public object assemble(serializable cached, object owner) throws hibernateexception { return cached; } @override public object deepcopy(object value) throws hibernateexception { return value; } @override public serializable disassemble(object value) throws hibernateexception { return (serializable) value; } @override 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; } }
wrote enhancer every attribute type
enum
, if type has static-method parseid. add following annotation javaassist:@type(type="hibernatehelper.genericenumusertype", parameters= { @parameter( name = "enumclass", value = "<fullqualified classname of enum class>"), })
but i'm unsure if not magic such problem. perhaps can give me advice.
Comments
Post a Comment