• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate(newbie) - generating relational database from Object model

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Say if we have an object model designed with 500+ classes which we want to persist to a relational database using hibernate, is there any standard way to do this? It might be easier to map each class to a separate table but from the database point of view I assume this would be a big performance hit. So the best way would be to map these class to relational tables. But to do this do we have to design the database first and then do the mapping or can we generate the database structure through OR-mapping tools?

All the examples I see are only two or three classes and the mapping could be customized manually by editing the hbm.xml files, but I wonder how to do this mapping in a large scale. Could someone elighten me with some existing tools that could generating this mapping? ofcourse with some examples would be good.

Thanks
Kevin
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try hibernate.hbbm2ddl.auto=update

You can find the documentation here:
http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html

 
Kevin Vicy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if I mentioned my question clear, I just have a bunch of persistable classes and dont have a relational database designed or hbm.xml mapping files written. I would like to know if there is a way/tool to generate the mapping files and the database from the persistable classes. And also would like to know the right way to do this, ie, do we need to design the relational database first and do the mapping?

Thanks
Kevin
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Hibernate comes with such a tool called, I think, SchemaExport. It goes all directions. For instance if you already had the database you can go the other way that you are looking for.

Good Luck.

Mark
 
Kevin Vicy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had looked through the SchemaUdate and SchemaExport functions, but those seem to require hbm.xml files written. I just have the object model and want to generate hbm.xml files as well as the database schema.

Thanks
Kevin
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, taken from Pro Hibernate 3 by Apress. In Hibernate 2 there is a CodeGenerator tool. This is no longer available in Hibernate 3. Because they suggest using Annotations in your POJOs.

Mark
 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your are going to have to user XDoclet if you need to be able to use Jave 1.4 or XDoclet or Annotations if you are using Java 5.0
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Bigbee:
Your are going to have to user XDoclet if you need to be able to use Jave 1.4 or XDoclet or Annotations if you are using Java 5.0



Actually, that isn't his question.

What he is asking, is for the name of the Hibernate tool that will convert his Java POJO DTO into a mapping file and/or also DDL for database?

XDoclet, just calls this tool, and in Hibernate 3.0 you have to use Annotations as this tool is no longer available.

Mark
 
Kevin Vicy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations...hmmm.... i spent some time trying to see what Annotations are and how it is useful. It seems to be sleek and get rids of mapping xml files but its kind of scary when they ask to use EJB3 persistence (what happened to the POJO's with no dependencies?). At least I dont think it requires a EJB container and can run with just a servlet container like Tomcat (correct me if i am wrong?). Also read somewhere that Annotations will negate the use of xdoclet! so better go with Annotations instead of trying to learn xdoclet. So has anyone played with Annotations and have comments on how better it is compared to xml mapping files or xdoclet?
Being a newbie to Hibernate, I am still not able to make a decision on whether to design the database and object model separately and do the mapping later (in which case i can just start with xml mapping) or should I do a bottom down approach to generate the mapping and database design from object model. Would be great if someone could clear this for me!

Thanks
Kevin
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.
It is really hard to put here an answer that will suite everybody. First you have to ask yourself whether your application will/should be available only for JDK 5.0 and will be running in EJB container. If your answer to both is yes then you might start considering use of Hibernate Annotations.

Yes with annotations there is no need of *.hbm.xml files and xdoclet2(note that xdoclet1 does not support Hibernate 3). The better thing for annotations is that they are compile safe, in the contrary when writing your mapping files(or generating them using xdoclet).Single typo in your mapping file can cost you valued time figuring out that is a problem.

There is one more very important thing for you guys. Currently Hibernate Annotation Configuration can be instantiated only programatically, that means you are now still not able to define it in the hibernate.cfg.xml.

Hopefully this helps.

Peter Laurinec
reply
    Bookmark Topic Watch Topic
  • New Topic