Forums Register Login

What hibernate.hbm2ddl.auto value to use?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
Try by removing this line

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
+Pie Number of slices to send: Send
No luck! It didn't make any difference. Data is still being deleted on each run.

Thanks,

Saeed
+Pie Number of slices to send: Send
Try using this:

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

--Tushar
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 42857 times.
Similar Threads
Hibernate Cannot insert values
No TransactionManagerLookup specified Exception
NullPointerException in creating Session Object using Hibernate
Which hibernate property is causing the existing data to be deleted?
exception on hibernate
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:10:13.