File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes What hibernate.hbm2ddl.auto value to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What hibernate.hbm2ddl.auto value to use?" Watch "What hibernate.hbm2ddl.auto value to use?" New topic
Author

What hibernate.hbm2ddl.auto value to use?

Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
Hi there,

I am almost 100% new to Hibernate but I'll try that my question makes sense.

I am following "Sebastian Hennebrueder's" tutorial and I have made the application to run successfully. Source code is here:

http://www.laliluna.de/download/first-hibernate-example-tutorial.zip

I have one issue though. When I run the application, Hibernate either drops and re-creates the table (<property name="hibernate.hbm2ddl.auto">create</property>) OR it deletes all the existing data (when <property name="hibernate.hbm2ddl.auto">validate</property>). I have tried removing this property altogether but Hibernate still deletes (truncates) the data in the table each time I run the test application. Here is the hibernate.cfg.xml:



And here is my Honey.hbm.xml:



Here is the test class:



Honey class is simple POJO and I am using MySQL to store the data. I hope I haven't missed any details.

Could someone please tell me what Hibernate configuration should I use so that when I run "TestHoney", old data remains in the table.

Thanks!

Saeed
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Just delete the "hibernate.hbm2ddl.auto" entry completely from you xml file. It's not required. That should make your problems go away.

And make sure you don't have more than one hibernate config file on your classpath. In that case, Hibernate might be going to the wrong file.

If you're a newbie, be sure to check out my signature links for some very easy to follow Hiberante tutorials.

Regards,

-Cameron McKenzie


Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
Thank you for your response Cameron!

I removed the line (previously I tried by commenting it out - I guess it is same as removing it) but the result is the same. In my TestHoney class (listed above), I am only inserting rows and then listing them. So, for each run, I should see 6 rows added to previous count but I only see 6-rows. Which means, this change made no difference. Here is modified hibernate.cfg.xml:



There is only one copy of this file in the classpath. Any other thoughts?

And yes, I am a newbie and I am absolutely going to go through your Hibernate guide/tutorial.

Thanks again and if there is anything else I may try to fix this problem, please do let me know.

Regards,

Saeed


Lalit Bhatt
Ranch Hand

Joined: Dec 27, 2007
Posts: 69

Try by removing this line

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


Java-JavaEE Hibernate Spring Software Development Business Process Analysis
Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
No luck! It didn't make any difference. Data is still being deleted on each run.

Thanks,

Saeed
Tushar Gupta
Greenhorn

Joined: Jun 05, 2009
Posts: 1
Try using this:

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

--Tushar
Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
Hi Tushar,

I have already tried this option too but it did not make any difference.

I solved the problem by replacing following line with the one below it:


Modified line:


So I am not sure which configuration OR properties file Hibernate was reading before this change. Perhaps that is why setting "hibernate.hbm2ddl.auto" to "update" didn't work.

I checked my classpath and there was no properties file and there was only one copy of configuration file (one copy of same file in the bin folder copied by Eclipse).

Do you know how can I check which file was Hibernate using?

Thank you!

Saeed
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Cameron Wallace McKenzie wrote:
And make sure you don't have more than one hibernate config file on your classpath. In that case, Hibernate might be going to the wrong file.


I just had a hunch!

You know, I might just do a search on your classpath for hibernate config files and see if there are any others. Your JVM must be picking one up from somewhere else on your classpath. This change is instructing the classloader to look local.

That's a very frustrating problem. I hope it doesn't sour your Hibernate experience. It really wasn't all Hibernate's fault.

Was there any source change management tool locking the updates and forcing you to use a previous version? I've seen that happen.

I am glad you got it working.

-Cameron McKenzie

Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
I am also glad it worked but I wonder the way I fixed it is the right way to do it or not?

I searched for properties and configuration files on my computer and there was no other copy of hibernate.cfg.xml except for the one in "src" folder and an identical copy in the "bin" folder (copied by Eclipse). The only copies of "hibernate.properties" files were the ones that came with the Hibernate distribution and these directories (where I unzipped them) are not on my classpath.

I still wonder if there is a way to "ask" hibernate which configuration/properties file it was look at (even if it was not hibernate's fault :-) )? I'll do some search to find the answer to this question but if you can point me in the right direction, it will save me valuable time.

Thank you Cameron and thank you everyone else who responded and helped.

Regards,
Saeed
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

At runtime, it will be picking up the copy in the bin directory. The editor works on the copy in the src directory.

I think there's a chance that the config file in the src directory wasn't being copied into the bin directory during build/compile time or runtime. Thus, your src and bin files may have been out of sync.

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What hibernate.hbm2ddl.auto value to use?