-1

Colleagues, I have HibernateUtil.class which returns sessionFactory:

public class HibernateUtil {
    private static final Logger LOG = Logger.getLogger(HibernateUtil.class.getName());
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from app.properties

            java.util.Properties properties = new Properties();
            properties.load(new FileInputStream("src//main//resources//app.properties"));
            Configuration configuration = new Configuration();

            configuration.configure("hibernate.cfg.xml").addProperties(properties);;

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);


        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            LOG.info("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

There are properties in app.properties:

hibernate.connection.url=jdbc:oracle:thin:@srv.te.loc:1521:DB
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.username=user
hibernate.connection.password=pass
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.current_session_context_class=thread

When I call test IDE shows:

02.02.2016 10:37:08.592 INFO  ApplicationDAOImpl:53 - Get application status for Application with № 00000000009240
02.02.2016 10:37:08.961 INFO  Version:37 - HHH000412: Hibernate Core {5.0.7.Final}
02.02.2016 10:37:08.965 INFO  Environment:213 - HHH000206: hibernate.properties not found
02.02.2016 10:37:08.968 INFO  Environment:317 - HHH000021: Bytecode provider name : javassist
02.02.2016 10:37:09.105 WARN  deprecation:91 - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
02.02.2016 10:37:09.931 WARN  deprecation:91 - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
02.02.2016 10:37:10.018 INFO  Version:66 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
02.02.2016 10:37:10.171 WARN  connections:71 - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
02.02.2016 10:37:10.452 INFO  connections:127 - HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@srv-pwcdb01.is.loc:1521:PWCDB]
02.02.2016 10:37:10.454 INFO  connections:136 - HHH10001001: Connection properties: {user=user, password=****}
02.02.2016 10:37:10.455 INFO  connections:141 - HHH10001003: Autocommit mode: false
02.02.2016 10:37:10.464 INFO  DriverManagerConnectionProviderImpl:39 - HHH000115: Hibernate connection pool size: 20 (min=1)
02.02.2016 10:37:10.795 INFO  Dialect:156 - HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
02.02.2016 10:37:11.311 INFO  ApplicationDAOImpl:65 - Session Is Opened :: true
02.02.2016 10:37:11.312 INFO  ApplicationDAOImpl:66 - Session Is Connected :: true
02.02.2016 10:37:11.325 WARN  QuerySplitter:124 - HHH000183: no persistent classes found for query class: from com.comp.db.entity.Applications where applicationNumber = :applicationNumber
02.02.2016 10:37:11.332 INFO  QueryTranslatorFactoryInitiator:47 - HHH000397: Using ASTQueryTranslatorFactory
org.hibernate.QueryParameterException: could not locate named parameter [applicationNumber]
    at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:131)
    at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:148)
    at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:517)
    at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:487)
    at ru.com.isbank.db.DAO.ApplicationDAOImpl.getPcoApplicationIBStatus(ApplicationDAOImpl.java:71)
    at ru.com.isbank.db.DAO.ApplicationDAOImplTest.testGetPcoApplicationIBStatus(ApplicationDAOImplTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.NullPointerException
    at ru.com.isbank.db.DAO.ApplicationDAOImpl.getApplicationStatus(ApplicationDAOImpl.java:87)
    at ru.com.isbank.db.DAO.ApplicationDAOImplTest.testGetApplicationStatus(ApplicationDAOImplTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

If I use the same properties (url, pass, dialect) in standard hibernate.cfg.xml and remove serviceRegistry from new Configuration().configure().buildSessionFactory(serviceRegistry) in HibernateUtil.class the same test works fine.

What is wrong with my properties file or with HibernateUtil class?

Also hibernate.cfg.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>



    <property name="connection.pool_size">10</property>
    <property name="hibernate.default_schema">defschema</property>
    <property name="show_sql">false</property>
    <property name="hibernate.connection.autocommit">false</property>

    <mapping class="com.comp.db.entity.Applications"/>
    <mapping resource="hbconfig/Applications.hbm.xml"></mapping>


  </session-factory>
</hibernate-configuration>
May12
  • 2,420
  • 12
  • 63
  • 99

2 Answers2

2

I think the problem lies here:

sessionFactory = new Configuration()...

Why do you instantiate a new Configuration instead of reusing the old one you initialized a few lines above?

Change to

sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);

EDIT:

Instead of using a ServiceRegistry just add the properties to the configuration as shown below:

static {
    try {
        // Create the SessionFactory from app.properties

        Properties properties = new Properties();
        properties.load(new FileInputStream("src//main//resources//app.properties"));
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        configuration.addProperties(properties);
        sessionFactory = configuration.buildSessionFactory();

    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        LOG.info("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

This works for me. Just debug to be sure your properties are correctly filled after loading.

Frank
  • 2,036
  • 1
  • 20
  • 32
0

Firstly, your way of configuration Hibernate 5 is incorrect. Reffer https://stackoverflow.com/a/32711654/3405171 for details.

To do configuration with a not standard properties using absolute path to the properties file you can do this

File propertiesPath = new File("some_path");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);  

Refer to the properties as a resource.

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties("app.properties").build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);  

You need to have app.properties in the class path.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67