-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate clears my tables when the application is started
PostPosted: Mon Oct 11, 2010 4:29 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 6:10 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
You use

<prop key="hibernate.hbm2ddl.auto">create</prop>

in hibernate config file, which would drop database tables and create them newly whenever you restart the application.

to avoid this: once all your tables are created then remove that property from config file


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

Joined: Tue Sep 28, 2010 3:20 am
Posts: 5
Thanks, the "create" value was indeed the cause,

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  
 
 Post subject: Re: Hibernate clears my tables when the application is started
PostPosted: Wed Oct 13, 2010 10:15 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Rick,

I am guessing that

you first load all A's inside the session and somehow close the session and then try to iterate A and trying to access B.

I can only guess what has happened as you dont post any of you code here

I suggest you to find the exact line of code where the B is loaded and before that line verify that you are in valid session (not closed one)

yes, hibernate's learning curve is quite steeper...:-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.