21

I tried to test whether the hibernate configuration is working properly. I tried but I got an error:

INFO: HHH000206: hibernate.properties not found

To do this: I create:

[1] hibernate configuration file [using xml]

<?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 name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.default_schema">explorecalifornia</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.connection.password">abc123</property>
 </session-factory>
</hibernate-configuration>

[2] A hibernate utility class

public class HibernateUtilities {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static{
        try{
            Configuration configuration = new Configuration().configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }

        catch(HibernateException exception){
            System.out.println("Problem creating session factory");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        HibernateUtilities.sessionFactory = sessionFactory;
    }


}

[3] The main program:

import org.hibernate.Session;

    public class Program {

        public static void main(String[] args) {
            System.out.println("Hibernate");

            Session session = HibernateUtilities.getSessionFactory().openSession();
            session.close();

        }

    }

But i got the following thing when I run the program:

Hibernate
Sep 29, 2013 10:47:15 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Problem creating session factory
Exception in thread "main" java.lang.NullPointerException
    at com.simpleprogrammer.Program.main(Program.java:10)

To solve this I tried Google and apply the ways which i found. but still i could not solve the problem. Can anybody please help me?

adarksun
  • 371
  • 1
  • 6
  • 20

8 Answers8

19

That's only an INFO message telling that you don't have a hibernate.properties file. This property file is not mandatory and so it's not what prevents your application from working.

If you want to know what caused the SessionFactory creation failure, you need to change your catch block to:

catch(HibernateException exception){
     System.out.println("Problem creating session factory");
     exception.printStackTrace();
}

You should use a logging framework instead.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
5

Your problem is creation session Factory for your application it happening in HibernateUtilities class , the reason may be due to , you cannot create a sessionfactory by sessionRegistry create it by hibernate Configuration class, because you registered your configuration in hibernate.cfg.xml

Just Replace the following code in HibernateUtilities class

        `sessionFactory = configuration.buildSessionFactory(serviceRegistry);`

to

        `sessionFactory = configuration.buildSessionFactory();`
kark
  • 4,763
  • 6
  • 30
  • 44
  • I replaced that line of code. when i run the program, I am getting the same error. "INFO: HHH000206: hibernate.properties not found" – adarksun Sep 30 '13 at 04:07
  • Just remove the line `serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();` in `UtilitiesClass` because in that part only your strucking and getting exception, and your all other configuration is quiet good – kark Sep 30 '13 at 04:11
  • I just tried your program, its working fine, did you set `properties file` in your console configuration , if yes, where you placed those. – kark Sep 30 '13 at 04:29
  • no i dont set / use any properties file. Moreover, i only use the following jars to do this: antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.2.Final.jar hibernate-core-4.2.5.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.1.Final.jar mysql-connector-java-5.0.8-bin.jar – adarksun Sep 30 '13 at 04:36
3

In my case, I was running the Hibernate 5 with JPA annotations on tomcat and stop working when I changed to glassfish 4.1

The error:

hibernate.properties not found

Make sure: src/main/resources/hibernate.cfg.xml exists

And if you only have the dependency of hibernate-core, I was using hibernate-annotations and hibernate-common-annotations and it was creating conflict. The hibernate 5 doesnt need these two, I had read somewhere.Try to remove ;)

After that a new error appears:

java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf(Ljava/lang/String;I)V

The reason was the oldest jboss-logging.jar at: "YOUR_GLASSFISH_FOLDER/glassfish/modules"

Why? The hibernate 5 has dependency with the newest version of jboss-logging, and the glassfish 4 uses the oldest version even if you declare inside your POM file the newest version. Actually I'm using:

org.jboss.logging jboss-logging 3.3.0.Final

Then I downloaded it and replace the old .jar inside the modules path and it back to work, I spent 2 days trying to solve that and I hope it helps some future issues =D

I used this link to help me: https://medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz

Another solution, in my case, could be back to the last Hibernate 4 version (4.3.11.Final) but it is already too old in my opinion

Vitorlui
  • 760
  • 1
  • 11
  • 26
1

The configuration files must be placed in the classpath. Make Sure that the hibernate.cfg.xml is in class-path. As your program is console program, u need to place that hibernate.cfg.xml in src folder. If u have placed it in other folder than specify it in Confingure.configure(fileName); And as answered By Mr.Kark u have to delete following lines

serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

kark
  • 4,763
  • 6
  • 30
  • 44
Hareesh
  • 694
  • 6
  • 18
1

You can try

  1. make sure that file hibernate.cfg.xml is located at src/main/resource
  2. add pom.xml

<!-- API, java.xml.bind module -->
  <dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
  </dependency>

<!-- Runtime, com.sun.xml.bind module -->
<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
  <version>2.3.2</version>
</dependency>
It work for me
0

I think you are missing some hibernate libs (maybe hibernate-tools.jar I guess). So please check it.

Anyway, I saw you using the hibernate annotations, so please try this code and see what happen with error properties not found.

try {
        AnnotationConfiguration configuration = new AnnotationConfiguration();          
        sessionFactory = configuration.configure().buildSessionFactory();       
    }
Ha Nguyen
  • 916
  • 6
  • 8
0

session-factory is a special tag in hibernate it's have no any name so, You can change your code session-factory name="" to session-factory and try to run.

0

In my case problem was in hibernate.cfg.xml location. Intellej IDEA created it in java folder. I've replaced it into resources folder and it helped

Barm
  • 1