Skip to content

Commit

Permalink
Fix some problems with Hibernate 5 configuration loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
v-ladynev committed Sep 22, 2015
1 parent 1bd29a5 commit 5074217
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<classpathentry kind="lib" path="libs/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="libs/geronimo-jta_1.1_spec-1.1.1.jar"/>
<classpathentry kind="lib" path="libs/hibernate-commons-annotations-5.0.0.Final.jar"/>
<classpathentry kind="lib" path="libs/hibernate-core-5.0.1.Final.jar"/>
<classpathentry kind="lib" path="libs/hibernate-core-5.0.1.Final.jar" sourcepath="/hibernate-orm/hibernate-core/src/main/java"/>
<classpathentry kind="lib" path="libs/hibernate-jpa-2.1-api-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="libs/jandex-1.2.2.Final.jar"/>
<classpathentry kind="lib" path="libs/javassist-3.18.1-GA.jar"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.property.access.spi.Getter;
import org.hibernate.property.access.spi.Setter;

/**
* Accesses property values via a get/set pair, which may be nonpublic. The default (and recommended
Expand All @@ -27,12 +25,12 @@
public class BasicIgnoreCasePropertyAccessor {

@SuppressWarnings("rawtypes")
public Setter getSetter(Class theClass, String propertyName) {
public BasicSetter getSetter(Class theClass, String propertyName) {
return createSetter(theClass, propertyName);
}

@SuppressWarnings("rawtypes")
private static Setter createSetter(Class theClass, String propertyName) {
private static BasicSetter createSetter(Class theClass, String propertyName) {
BasicSetter result = getSetterOrNull(theClass, propertyName);
if (result == null) {
throw new PropertyNotFoundException(String.format(
Expand Down Expand Up @@ -114,12 +112,12 @@ private static Method setterMethod(Class theClass, String propertyName) {
}

@SuppressWarnings("rawtypes")
public Getter getGetter(Class theClass, String propertyName) {
public BasicGetter getGetter(Class theClass, String propertyName) {
return createGetter(theClass, propertyName);
}

@SuppressWarnings("rawtypes")
public static Getter createGetter(Class theClass, String propertyName) {
public static BasicGetter createGetter(Class theClass, String propertyName) {
BasicGetter result = getGetterOrNull(theClass, propertyName);
if (result == null) {
throw new PropertyNotFoundException(String.format(
Expand Down Expand Up @@ -181,7 +179,7 @@ private static PropertyDescriptor[] getPropertyDescriptors(Class<?> beanClass) {
}

/** Basic setter. */
public static final class BasicSetter implements Setter {
public static final class BasicSetter {
private static final long serialVersionUID = -3092319284832094425L;
@SuppressWarnings("rawtypes")
private final Class clazz;
Expand All @@ -200,7 +198,6 @@ private BasicSetter(Class clazz, Method[] getMethods, Method[] setMethods, Metho
this.setMethods = setMethods;
}

@Override
@SuppressWarnings("rawtypes")
public void set(Object target, Object value, SessionFactoryImplementor factory)
throws HibernateException {
Expand Down Expand Up @@ -243,23 +240,21 @@ public void set(Object target, Object value, SessionFactoryImplementor factory)
}
String errorMessage = String.format("Expected type: %s, actual value: %s", method
.getParameterTypes()[0].getName(), value == null ? null : value.getClass()
.getName());
.getName());
throw new PropertyAccessException(iae, errorMessage, true, clazz, propertyName);
} catch (Exception e) {
String errorMessage = String.format(
"Setter information: expected type: %s, actual type: %s", method
.getParameterTypes()[0].getName(), value == null ? null : value
.getParameterTypes()[0].getName(), value == null ? null : value
.getClass().getName());
throw new PropertyAccessException(e, errorMessage, true, clazz, propertyName);
}
}

@Override
public Method getMethod() {
return method;
}

@Override
public String getMethodName() {
return method.getName();
}
Expand All @@ -275,7 +270,7 @@ public String toString() {
}

/** Basic getter. */
public static final class BasicGetter implements Getter {
public static final class BasicGetter {
private static final long serialVersionUID = 2828591409798958202L;
@SuppressWarnings("rawtypes")
private final Class clazz;
Expand All @@ -289,7 +284,6 @@ private BasicGetter(Class clazz, Method method, String propertyName) {
this.propertyName = propertyName;
}

@Override
public Object get(Object target) throws HibernateException {
try {
return method.invoke(target, (Object[]) null);
Expand All @@ -305,29 +299,24 @@ public Object get(Object target) throws HibernateException {
}
}

@Override
@SuppressWarnings("rawtypes")
public Object getForInsert(Object target, Map mergeMap, SessionImplementor session) {
return get(target);
}

@Override
@SuppressWarnings("rawtypes")
public Class getReturnType() {
return method.getReturnType();
}

@Override
public Method getMethod() {
return method;
}

@Override
public String getMethodName() {
return method.getName();
}

@Override
public Member getMember() {
return method;
}
Expand Down
27 changes: 23 additions & 4 deletions src/com/github/fluent/hibernate/HibernateSessionFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.fluent.hibernate;

import java.io.File;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
Expand Down Expand Up @@ -34,14 +36,31 @@ private static void assertSessionFactory() {
}

public synchronized static void createSessionFactory(String hibernateCfgXml) {
createSessionFactory(hibernateCfgXml, null);
}

public synchronized static void createSessionFactory(String hibernateCfgXml, File propertiesPath) {
if (sessionFactory != null) {
return;
}

Configuration ac = new Configuration().configure(hibernateCfgXml);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
ac.getProperties()).build();
sessionFactory = ac.buildSessionFactory(serviceRegistry);
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder()
.configure(hibernateCfgXml);

if (propertiesPath != null) {
serviceRegistryBuilder.loadProperties(propertiesPath);
}

sessionFactory = createSessionFactory(serviceRegistryBuilder.build());
}

private static SessionFactory createSessionFactory(ServiceRegistry serviceRegistry) {
try {
return new Configuration().buildSessionFactory(serviceRegistry);
} catch (Throwable th) {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
throw new RuntimeException(th);
}
}

public synchronized static void closeSessionFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.transform.ResultTransformer;

import com.github.fluent.hibernate.BasicIgnoreCasePropertyAccessor.BasicSetter;

/**
* @author DoubleF1re
* @author V.Ladynev
Expand All @@ -19,7 +20,7 @@ public class IgnoreCaseAliasToBeanResultTransformer implements ResultTransformer
@SuppressWarnings("rawtypes")
private final Class resultClass;

private Setter[] setters;
private BasicSetter[] setters;

private final BasicIgnoreCasePropertyAccessor propertyAccessor;

Expand Down Expand Up @@ -61,7 +62,7 @@ public Object transformTuple(Object[] tuple, String[] aliases) {

try {
if (setters == null) {
setters = new Setter[aliases.length];
setters = new BasicSetter[aliases.length];
for (int i = 0; i < aliases.length; i++) {
String alias = aliases[i];
if (alias != null) {
Expand Down
12 changes: 12 additions & 0 deletions tests/com/github/fluent/hibernate/HibernateSessionFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ private static Configuration constructConfiguration() {
Configuration configuration = new Configuration();
configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
// configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
/*
cfg=new Configuration();
Properties p = new Properties();
p.put( Environment.DIALECT, "org.hibernate.dialect.HSQLDialect" );
p.put( "hibernate.connection.driver_class", "org.h2.Driver" );
p.put( "hibernate.connection.url", "jdbc:h2:mem:" );
p.put( "hibernate.connection.username", "sa" );
p.put( "hibernate.connection.password", "" );
cfg.setProperties(p);
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
*/

return configuration;
}

Expand Down

0 comments on commit 5074217

Please sign in to comment.