Skip to content

Commit

Permalink
PostgreSQLEnumType is not compatible with Hibernate 5.2 because of Ja…
Browse files Browse the repository at this point in the history
…vaTypeDescriptorRegistry and TypeConfiguration vladmihalcea#176
  • Loading branch information
vladmihalcea committed Jan 21, 2020
1 parent dd648d5 commit aeb9db9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
3 changes: 2 additions & 1 deletion hibernate-types-52/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
</dependencies>

<properties>
<hibernate.version>5.4.9.Final</hibernate.version>
<hibernate.version>5.4.10.Final</hibernate.version>
<!--<hibernate.version>5.2.18.Final</hibernate.version>-->
<postgresql.version>9.4-1202-jdbc41</postgresql.version>

<mysql.version>8.0.13</mysql.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.vladmihalcea.hibernate.type.basic;

import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ReflectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor;
import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;

import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand Down Expand Up @@ -49,17 +48,25 @@ public PostgreSQLEnumType(Configuration configuration) {
public PostgreSQLEnumType(Class<? extends Enum> enumClass) {
this();

setTypeConfiguration(new TypeConfiguration() {
@Override
public JavaTypeDescriptorRegistry getJavaTypeDescriptorRegistry() {
return new JavaTypeDescriptorRegistry(this) {
@Override
public EnumJavaTypeDescriptor getDescriptor(Class javaType) {
return new EnumJavaTypeDescriptor(enumClass);
}
};
}
});
Class typeConfigurationClass = ReflectionUtils.getClassOrNull("org.hibernate.type.spi.TypeConfiguration");

if(typeConfigurationClass != null) {
Object typeConfiguration = ReflectionUtils.newInstance(typeConfigurationClass);

Class enumJavaTypeDescriptorClass = ReflectionUtils.getClassOrNull("org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor");

Object enumJavaTypeDescriptor = ReflectionUtils.newInstance(enumJavaTypeDescriptorClass, new Object[] {enumClass}, new Class[]{enumClass.getClass()});

Object javaTypeDescriptorRegistry = ReflectionUtils.invokeGetter(typeConfiguration, "javaTypeDescriptorRegistry");

ReflectionUtils.invokeMethod(
javaTypeDescriptorRegistry,
ReflectionUtils.getMethod(javaTypeDescriptorRegistry, "addDescriptor", JavaTypeDescriptor.class),
enumJavaTypeDescriptor
);

ReflectionUtils.invokeSetter(this, "typeConfiguration", typeConfiguration);
}

Properties properties = new Properties();
properties.setProperty("enumClass", enumClass.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ public static <T> T newInstance(Class clazz) {
}
}

/**
* Instantiate a new {@link Object} of the provided type.
*
* @param clazz The Java {@link Class} of the {@link Object} to instantiate
* @param args The arguments that need to be passed to the constructor
* @param argsTypes The argument types that need to be passed to the constructor
* @param <T> class type
* @return new Java {@link Object} of the provided type
*/
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class clazz, Object[] args, Class[] argsTypes) {
try {
Constructor<T> constructor = clazz.getDeclaredConstructor(argsTypes);

return constructor.newInstance(args);
} catch (InstantiationException e) {
throw handleException(e);
} catch (IllegalAccessException e) {
throw handleException(e);
} catch (NoSuchMethodException e) {
throw handleException(e);
} catch (InvocationTargetException e) {
throw handleException(e);
}
}

/**
* Get the {@link Field} with the given name belonging to the provided Java {@link Class}.
*
Expand Down

0 comments on commit aeb9db9

Please sign in to comment.