Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Let Hibernate generate tables ?

 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am ok with database design�

But most of the hibernate tutorials and books that I read design the Java classes and let Hibernate create the DB tables and all.

I have already hand-crafted my DB design and all the tables in MySQL. I am not sure whether to throw all that away and let Hibernate generate all tables from java classes.

I am currently starting to map Java classes to my existing DB design/tables. Manually mapping Java�s �short� to MySQL�s �smallint� � for example - one-by-one.

If I "throw" all my hand-crafted DB design and tables created...I feel that it is a waste.

Please advice.

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though the tools you can get to allow Hibernate to generate your schema do have their uses, I'm not a fan of letting Hibernate decide how my ER model should look. My personal preference is to do the data modelling just as you have done, generate my mappings/pojos from it then edit them by hand till they fit.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock, thanks for the reply.

generate my mappings/pojos from it then edit them by hand till they fit.


Wish to clarify. You mean this sequence ? :
1stly, hand code/hand design DDL SQL - tables/foreign key reference integrity and all
2ndly, generate hbm.xml mapping files and Java pojos from number 1.
3rdly, make manual adjustments to any of the 3 above (DB, hbm,xml and java pojos) where necessary.

Is this the way ...how you would normally do things ?

For step 2, what tool(s) do you use for the generation work ?

Thanks.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just feel very venerable as just a simple hbm2ddl set to "update"/"create"/"create-drop" and my hand-coded DB DDL codes (tables/foreign key reference integrity etc) will be wiped off and re-written by Hibernate.

If I want to preserve what I have coded in my DB side (which is my starting point as mentioned by Sturrock)... my hbm2ddl has to be set to "validate" and only "validate" all the time ?
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually...I am rather confused between "validate" and "update" for hbm2ddl.auto.

Can someone explain the difference to me ?

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using hbm2dll you must be using Hibernate2. There are a whole new set of tools (see here) you can use.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting a new project, so...I am using the latest Hibernate which is... 3.1
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hbm2dll is not going to work with Hibernate3. It is no longer included as part of the Hibernate download, nor is it part of the tools download. All the older tools were deprecated in favour of the new eclipse integration.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sturrock, thanks for the help and information.

But what I mean by hbm2ddl in my post is not the hbm2ddl tool...I am referring to the hbm2ddl.auto property in the hibernate.cfg.xml file :
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still interested to know the answers for my questions I asked above regarding the hbm2ddl.auto property in hibernate.cfg.xml.

Sturrock, sorry if I didn't state my question clear enough.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops, I need to correct my previous post. hbm2ddl is included in the core Hibernate3 download. Sorry for the confusion; its just no longer the recommended route to perform this task. See here.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though the tools you can get to allow Hibernate to generate your schema do have their uses, I'm not a fan of letting Hibernate decide how my ER model should look. My personal preference is to do the data modelling just as you have done, generate my mappings/pojos from it then edit them by hand till they fit.


If you generate (hbm.xml and .java) and then edit them manually... when you need to generate again, all your manual changes will be gone. How do you solve this problem ?

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't generate them again. Once the bulk of the work creating the mappings and pojo classes have been done I don't see a need to regenerate. I'd hope all that was needed from then on would be small manual changes.
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock, thanks for answering my question !

hbm2dll.auto should always set to "validate" and only "validate" if I start with/hand-crafted my DB schema and don't want hibernate to "touch" it even a bit ? Do you do this ?

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic