added missing class GenericEnumType
This commit is contained in:
@ -27,7 +27,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-api</artifactId>
|
<artifactId>log4j-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<type>jar</type>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>javaee-api</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
|
|||||||
@ -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<T, E extends Enum<E>> implements UserType {
|
||||||
|
|
||||||
|
private int sqlType;
|
||||||
|
private Class<E> clazz = null;
|
||||||
|
private HashMap<String, E> enumMap;
|
||||||
|
private HashMap<E, String> valueMap;
|
||||||
|
|
||||||
|
public GenericEnumType(Class<E> 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<E> returnedClass() {
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] sqlTypes() {
|
||||||
|
return new int[]{sqlType};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user