2

I am using create-drop for hibernate and its not working. I am restarting my project but still its not dropping my old table and its data.

<property name="hibernate.hbm2ddl.auto">create-drop</property>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/HibernateConfiguration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.password">****</property>

<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- Names the annotated entity class -->
<mapping class="domains.Test"/>
<mapping class="domains.LoginDetails"/>
<mapping class="domains.User"/>
</session-factory>
</hibernate-configuration>

I found that, I don't know if I am wrong or correct. While I was running my application hibernate was not dropping any table nor creating any table even though create-drop is mentioned in configuration file. But when I created one new object and save method is called hibernate dropped all tables and created all tables again. So question raised in my mind that may be hibernate is only performing operation when there is a sessionFactory object is there. I am using Singleton pattern to create sessionFacotry object. So until I am not doing any CRUD operation sessionFactory object will not be created and hence it was not dropping or recreating all tables. Please correct me if I am wrong.

public static SessionFactory sessionFactory;

private HibernateUtility(){}

public static SessionFactory getSessionFactory() {
    try{
        if (sessionFactory == null) {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
    }catch(Exception e) {
        System.out.println(e);
    }

    return sessionFactory;
}

Until I am calling this method from DAO hibernate not doing any create or drop table operation. This is what I am trying to say. Until sessionFactory object is not created create-drop not working

Aks 1316
  • 372
  • 5
  • 19
  • Possible duplicate of [Schema is not dropped on hbmddl.auto = create.drop](https://stackoverflow.com/questions/6751090/schema-is-not-dropped-on-hbmddl-auto-create-drop). – Sergey Vyacheslavovich Brunov Jan 27 '18 at 17:41
  • @SergeyBrunov Sorry I didn't get what you are trying to say. – Aks 1316 Jan 27 '18 at 18:02
  • Could you please take a look at the [answer](https://stackoverflow.com/a/32491793/)? What do you think about it? – Sergey Vyacheslavovich Brunov Jan 27 '18 at 18:10
  • 1
    @SergeyBrunov I have checked that answer and I have closed sessionFactory properly. But one more question came in my mind correct me if I am wrong. Hibernate will create-drop table when sessionFactory will be created right and when I will stop tomcat it will automatically destroy sessionFactory object. Is it correct? – Aks 1316 Jan 27 '18 at 18:45
  • Could you please provide (post) a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – Sergey Vyacheslavovich Brunov Jan 27 '18 at 18:47
  • 1
    @SergeyBrunov What happened here, When I was starting server or running my application it was not creating or dropping tables. But when I created new user which requires sessionFactory object hibernate dropped and created all tables. I have used singleton design pattern to create sessionFactory object. So until I am doing any Db operation its not creating sessionFactory object and may be that is reason tables were not dropped or created while running just application. Because just running application will not create sessionFactory object. – Aks 1316 Jan 27 '18 at 19:11
  • It is necessary to see the example to get better understanding. Could you please post it? – Sergey Vyacheslavovich Brunov Jan 27 '18 at 19:15
  • Please feel free to update the question instead of adding comments: add the (full) context there. – Sergey Vyacheslavovich Brunov Jan 27 '18 at 20:10

1 Answers1

2

Analysis

(Note: This is just a guess.)

It seems an instance of the SessionFactory interface is not getting closed (by Tomcat?).

Solution

It is necessary to configure Tomcat to manage the Hibernate's SessionFactory appropriately. Please refer to:

  1. Using Hibernate with Tomcat |JBoss Developer.
  2. TomcatHibernate - Tomcat Wiki.
  3. Setup Hibernate Session Factory in Tomcat - Stack Overflow.
  4. dev - Tomcat, JPA, Hibernate and MySQL. The related gist: Integrate Hibernate 4.1 in a raw Tomcat 7 (no JPA, no Spring, etc) · GitHub.
  5. configure hibernate with embedded tomcat 7 programmatically - Stack Overflow.