0

I have created a separate utility class for SessionFactory and below is my code:

        package com.Hibernate.Util;

        import org.hibernate.SessionFactory;
        import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
        import org.hibernate.cfg.Configuration;
        import org.hibernate.service.ServiceRegistry;

        public class HibernateUtil {
            private static SessionFactory sessionFactory;
            private static SessionFactory buildSessionFactory() {
                try {
                    Configuration configuration = new Configuration();
                    configuration.configure("hibernate.cfg.xml");
                    System.out.println("Hibernate configuration loading......");
                    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties()).build();
                    System.out.println("Hibernate serviceRegistry created.....");
                    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
                    return sessionFactory;
                } catch (Throwable ex) {
                    System.out.println("SessionFactory creation failure.....");
                    ex.printStackTrace();
                    throw new ExceptionInInitializerError(ex);
                }
            }
            public static SessionFactory getSessionFactory1() {
                if (sessionFactory == null)
                    sessionFactory = buildSessionFactory();
                return sessionFactory;
            }
        }

Hibernate configuration file hibernate.cfg.xml file:

    <?xml version="1.0" encoding="utf-8"?>
     <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
     <hibernate-configuration>
      <session-factory> 
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    
            <property name="hibernate.connection.username">root</property>    
            <property name="hibernate.connection.password">root.123</property>    
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Hibernate</property>    
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
            <property name="hibernate.current_session_context_class">thread</property>
            <property name="hibernate.hbm2ddl.auto">create</property>   
            <property name="hibernate.show_sql">true</property>    
            <mapping class="com.Hibernate.Model.Employee"></mapping>
            <mapping class="com.Hibernate.Model.Address"></mapping>
       </session-factory> 
     </hibernate-configuration>

Below class calls the SessionFactory method buildSessionFactory HQLExample.java:

package com.Hibernate.Main;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.Hibernate.Model.Employee;
import com.Hibernate.Util.HibernateUtil;

public class HQLExample {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory1();
        System.out.println(sessionFactory);
        Session session = sessionFactory.getCurrentSession();
    }
}

The code is executing but the tables are being not created. Can someone please tell me what would be the issue with the code???

Akshay
  • 1,161
  • 1
  • 12
  • 33
Ranju Pillai
  • 51
  • 1
  • 1
  • 8
  • I didnt quite get your question??what exactly do you mean by table not getting created? – Akshay Mar 06 '17 at 08:59
  • I have created two Entity class: Employee and Address. The classes have been mapped to the hibernate.cfg.xml file. But while executing the Main class there needs to be creation of two table, but in my case no table are not creating. I don't know what exactly is going wrong. Please help me out as I am new to Hibernate technology. – Ranju Pillai Mar 06 '17 at 09:33
  • Can please check in logs, is there any create query ? Why I'm asking as I can see you enabled show_sql in xml config and might be access issue – mihatel Mar 06 '17 at 10:29
  • I can see any create query statement in log, but there some statement like hibernate.properties not found INFO: HHH000412: Hibernate Core {5.0.12.Final} Mar 06, 2017 3:37:55 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found If following statement is added then table is created in database: SessionFactory factory = new Configuration().configure().buildSessionFactory(); – Ranju Pillai Mar 06 '17 at 10:30
  • If no exception/error then only guess might be you should specify MySQL5InnoDBDialect instead of MySQLDialect if your mysql not prior version 5.x [DOC](https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/dialect/package-summary.html) – mihatel Mar 06 '17 at 10:38
  • Changed to MySQL5InnoDBDialect but still the table is not created. – Ranju Pillai Mar 06 '17 at 10:43
  • Please have a look into this issue [http://stackoverflow.com/questions/19085816](http://stackoverflow.com/questions/19085816/how-to-fix-the-error-info-hhh000206-hibernate-properties-not-found) – mihatel Mar 06 '17 at 10:47
  • The issue is with the hibernate.cfg.xml file. It is not mapping the classes Employee and Address even though including it into the file. Any idea how to map the classes? – Ranju Pillai Mar 06 '17 at 11:32
  • The file hibernate.cfg.xml is placed at src/main/resources location of the project. – Ranju Pillai Mar 06 '17 at 11:51
  • hey, I meet the same situation – nimbus_debug Jan 31 '19 at 09:05

1 Answers1

0

Please update the main method using the below codebase in HQLExample.java:

public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory1();
        System.out.println(sessionFactory);
        Session session = sessionFactory.getCurrentSession();
   try {
            Transaction transaction = session.beginTransaction();
            transaction.commit();
            System.out.println("Database tables created successfully");

        } catch (Exception e) {
            System.out.println("Error to creating database tables for hibernate file : "+ e.getMessage());
        }finally {
           sessionFactory.close();
           session.close();
        }
    }