-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: buildSessionFactory deletes my table
PostPosted: Fri Mar 14, 2008 10:16 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Hibernate version:
3.0
Mapping documents:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
   
        <!-- Settings for a local HSQL (testing) database. -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/dbname</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>

        <!-- Use the C3P0 connection pool. -->
        <property name="c3p0.min_size">3</property>
        <property name="c3p0.max_size">5</property>
        <property name="c3p0.timeout">1800</property>
   
        <!-- Disable second-level cache. -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="cache.use_query_cache">false</property>
        <property name="cache.use_minimal_puts">false</property>
        <property name="max_fetch_depth">3</property>
   
        <!-- Print SQL to stdout. -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
   
        <!-- Drop and then re-create schema on SessionFactory build, for testing. -->
        <property name="hbm2ddl.auto">create</property>
   
        <!-- Bind the getCurrentSession() method to the thread. -->
        <property name="current_session_context_class">thread</property>

        <!-- Hibernate XML mapping files -->
        <mapping resource="test/TestDAO.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

Code:
Code:
public class HibernateUtil {
   
   private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

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

Name and version of the database you are using:
MySQL 5.0

When I run my application with an empty database things appear to work and I'm able to add data to my one and only table. However, if I run my application again the data in the table is removed when I reach the code:
Code:
sessionFactory = new Configuration().configure().buildSessionFactory();
Any ideas on why this may be happening? I'm not sure if this is important but this line of code takes an excessive amount of time to execute, maybe 30 seconds.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 10:18 pm 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
Have a look at your config, it's already documented what is happening...:-)

...
<!-- Drop and then re-create schema on SessionFactory build, for testing. -->
<property name="hbm2ddl.auto">create</property>
...

Better to use:

<property name="hbm2ddl.auto">update</property>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 15, 2008 3:22 pm 
Newbie

Joined: Mon Jan 28, 2008 1:55 pm
Posts: 19
Wow, you mean all I had to do was read the XML comments??? Thanks for pointing out what should have been obvious, it worked!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.