diff --git a/jeeutil/pom.xml b/jeeutil/pom.xml index 32e68f1..e9d3a89 100644 --- a/jeeutil/pom.xml +++ b/jeeutil/pom.xml @@ -27,7 +27,12 @@ org.apache.logging.log4j log4j-api - + + + org.hibernate + hibernate-core + jar + javax javaee-api diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java new file mode 100644 index 0000000..58315a2 --- /dev/null +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java @@ -0,0 +1,114 @@ +package de.muehlencord.shared.jeeutil; + +import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import org.hibernate.HibernateException; +import org.hibernate.usertype.UserType; + +/** + * http://octagen.at/2014/10/postgresql-custom-data-types-enum-in-hibernate/ + * + * @author joern@muehlencord.de + */ +public abstract class GenericEnumType> implements UserType { + + private int sqlType; + private Class clazz = null; + private HashMap enumMap; + private HashMap valueMap; + + public GenericEnumType(Class clazz, E[] enumValues, String method, int sqlType) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + this.clazz = clazz; + enumMap = new HashMap<>(enumValues.length); + valueMap = new HashMap<>(enumValues.length); + Method m = clazz.getMethod(method); + + for (E e : enumValues) { + + @SuppressWarnings("unchecked") + T value = (T) m.invoke(e); + + enumMap.put(value.toString(), e); + valueMap.put(e, value.toString()); + } + this.sqlType = sqlType; + } + + @Override + public Object assemble(Serializable cached, Object owner) + throws HibernateException { + return cached; + } + + @Override + public Object deepCopy(Object obj) throws + HibernateException { + return obj; + } + + @Override + public Serializable disassemble(Object obj) throws + HibernateException { + return (Serializable) obj; + } + + @Override + public boolean equals(Object obj1, Object obj2) throws HibernateException { + if (obj1 == null && obj2 == null) { + return true; + } else if (obj1 != null && obj2 == null) { + return false; + } else { + return obj1.equals(obj2); + } + } + + @Override + public int hashCode(Object obj) throws HibernateException { + return obj.hashCode(); + } + + @Override + public boolean isMutable() { + return false; + } + + public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { + String value = rs.getString(names[0]); + if (!rs.wasNull()) { + return enumMap.get(value); + } + return null; + } + + public void nullSafeSet(PreparedStatement ps, Object obj, int index) throws HibernateException, SQLException { + if (obj == null) { + ps.setNull(index, sqlType); + } else { + ps.setObject(index, valueMap.get(obj), sqlType); + } + } + + @Override + public Object replace(Object original, Object target, Object owner) + throws HibernateException { + return original; + } + + @Override + public Class returnedClass() { + return clazz; + } + + @Override + public int[] sqlTypes() { + return new int[]{sqlType}; + } + +}