-->
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: Hibernate clears my tables when the application is started
PostPosted: Mon Oct 11, 2010 4:48 am 
Newbie

Joined: Tue Sep 28, 2010 3:20 am
Posts: 5
My objects are stored in the database correctly, however, when I restart the application, all tables in my database (mysql) are always cleared.
During runtime I can see that the database if filled correctly.
When I quit the application, the database still contains all objects.
After I restart however, I see that all tables are cleared from the database. At that moment the application has not done anything with hibernate. Somehow, when the sessionFactory is created, it first clears the tables....
I use the following settings for the sessionFactory (Spring):

Code:
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
      <props>

        <prop key="hibernate.hbm2ddl.auto">create</prop>
        <prop key="hibernate.connection.pool_size">1</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.connection.autocommit">true</prop>


What am I doing wrong?
What do I have to do such that hibernate will not clear the tables when my application is started?


Top
 Profile  
 
 Post subject: Re: Hibernate clears my tables when the application is started
PostPosted: Mon Oct 11, 2010 5:45 am 
Newbie

Joined: Fri Sep 24, 2010 8:20 am
Posts: 5
change this line to "validate" instead of "create" and you'll be fine.

Code:
<prop key="hibernate.hbm2ddl.auto">validate</prop>


[create] means that with every init of the Session Factory all tables will be dropped and recreated.
[validate] means that with every init of the Session Factory the schema will be validated against your annotations/hbm.xml files

Hope this helps
Regards Michael


Top
 Profile  
 
 Post subject: Re: Hibernate clears my tables when the application is started
PostPosted: Mon Oct 11, 2010 10:19 am 
Newbie

Joined: Tue Sep 28, 2010 3:20 am
Posts: 5
Hi Michael,
thanks, that indeed solved that problem.
Now when the application is restarted, I get a "lazy initialisation" exception, saying that the session was either not open or closed....
My domain structure looks something like this: A->B->C. A contains a List<B>, "B" contains a List<C>.
I load the toplevel objects with: List<A> allA = getHibernateTemplate().loadAll(class.A);
When, somewhere later in the code B is lazy loaded (because I iterate the list, which causes the delayed loading) the exception happens.
The message of the exception is "no session or session closed". It did load parts of the structure, so there is a session I guess. I don't know why it is saying that there is no session. The parts of the structure that have been ïnstantiated looks ok.

The hibernate learning curve is steeper than I thought. It is often hard to find the reason of a failure....

Rick


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.