Hibernate ing Enumeration type in JSP, hibernate Enumeration
Hibernate ing Enumeration type in JSP
Problem:
Java BO Gender is an enumeration type. How to compile hbm. xml if you want to save it as a string in the database?
public enum Gender{ UNKNOWN("Unknown"), MALE("Male"), FEMALE("Female"); private String key; private Gender(final String key) { this.key = key; } public getGender(String key) { for (Gender gender : Gender.values()) { if (key.euqals(gender.getKey())) return gender; } throw new NoSuchElementException(key); } }
Use UserType:
Public class GenderUserType implements UserType {private static int [] typeList = {Types. VARCHAR};/** Return the SQL type codes for the columns mapped by this type. * The codes are defined on <tt> java. SQL. types </tt>. * // ** set the SQL type of the field corresponding to the sex attribute of Gender Class */public int [] sqlTypes () {return typeList ;} /* The class returned by <tt> nullSafeGet () </tt>. * // ** set the Java class mapped by GenderUserType: Gender Class */public C Lass returnedClass () {return Gender. class;}/** indicates that the Gender class is an unchangeable class */public boolean isMutable () {return false;}/** Return a deep copy of the persistent state, stopping at entities and at * collections. it is not necessary to copy immutable objects, or null * values, in which case it is safe to simply return the argument. * // ** return the Gender Object snapshot. Because the Gender class is an immutable class, the Gender Object represented by the parameter is directly returned */public Object DeepCopy (Object value) {return (Gender) value;}/** compare whether a Gender Object is the same as its snapshot */public boolean equals (Object x, Object y) {// because only two static constant Gender instances are available in the memory, // you can directly compare return (x = y) by memory address;} public int hashCode (Object x) {return x. hashCode ();}/** Retrieve an instance of the mapped class from a JDBC resultset. implementors * shocould handle possibility of null values. * // ** read the key from the JDBC ResultSet and return the corresponding Gender instance */public Object nullSafeGet (ResultSet rs, String [] names, Object owner) throws HibernateException, SQLException {// read key String sex = (String) Hibernate from the ResultSet. STRING. nullSafeGet (rs, names [0]); if (sex = null) {return null;} // search for a Gender instance by Gender. try {return Gender. getGender (sex);} catch (java. util. noSuchElementException e) {throw new HibernateException ("Bad Gender value:" + sex, E) ;}}/** Write an instance of the mapped class to a prepared statement. implementors * shocould handle possibility of null values. * A multi-column type shocould be written to parameters starting from <tt> index </tt>. * // ** Add the key attribute of the Gender Object to JDBC PreparedStatement */public void nullSafeSet (PreparedStatement st, Object value, int index) throws HibernateException, SQLException {String sex = null; If (value! = Null) sex = (Gender) value ). getKey (); Hibernate. string. nullSafeSet (st, sex, index);}/** Reconstruct an object from the cacheable representation. at the very least this * method shocould perform a deep copy if the type is mutable. (optional operation) */public Object assemble (Serializable cached, Object owner) {return cached;}/** Transform the object into its cacheable representation. at the very least this * method shocould perform a deep copy if the type is mutable. that may not be enough * for some implementations, however; for example, associations must be cached as * identifier values. (optional operation) */public Serializable disassemble (Object value) {return (Serializable) value;}/** During merge, replace the existing (target) value in the entity we are merging to * with a new (original) value from the detached entity we are merging. for immutable * objects, or null values, it is safe to simply return the first parameter. for * mutable objects, it is safe to return a copy of the first parameter. for objects * with component values, it might make sense to recursively replace component values. */public Object replace (Object original, Object target, Object owner) {return original ;}}
Then define the ing in hbm. xml:
Extension:
Defining a UserType for each Enumeration type is troublesome. You can define an abstract class.
For example, extend the following example to apply to all the enumerated types saved as index.
public abstract class OrdinalEnumUserType<E extends Enum<E>> implements UserType { protected Class<E> clazz; protected OrdinalEnumUserType(Class<E> clazz) { this.clazz = clazz; } private static final int[] SQL_TYPES = {Types.NUMERIC}; public int[] sqlTypes() { return SQL_TYPES; } public Class<?> returnedClass() { return clazz; } public E nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException { //Hibernate.STRING.nullSafeGet(rs, names[0]) int index = resultSet.getInt(names[0]); E result = null; if (!resultSet.wasNull()) { result = clazz.getEnumConstants()[index]; } return result; } public void nullSafeSet(PreparedStatement preparedStatement, Object value,int index) throws HibernateException, SQLException { if (null == value) { preparedStatement.setNull(index, Types.NUMERIC); } else { //Hibernate.String.nullSafeSet(st, sex, index); preparedStatement.setInt(index, ((E)value).ordinal()); } } public Object deepCopy(Object value) throws HibernateException{ return value; } public boolean isMutable() { return false; } public Object assemble(Serializable cached, Object owner) throws HibernateException { return cached; } public Serializable disassemble(Object value) throws HibernateException { return (Serializable)value; } public Object replace(Object original, Object target, Object owner) throws HibernateException { return original; } public int hashCode(Object x) throws HibernateException { return x.hashCode(); } public boolean equals(Object x, Object y) throws HibernateException { if (x == y) return true; if (null == x || null == y) return false; return x.equals(y); } }
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!