wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate(newbie) - generating relational database from Object model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate(newbie) - generating relational database from Object model" Watch "Hibernate(newbie) - generating relational database from Object model" New topic
Author

Hibernate(newbie) - generating relational database from Object model

Kevin Vicy
Greenhorn

Joined: Aug 21, 2005
Posts: 6
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
Thomas Bigbee
Ranch Hand

Joined: Nov 29, 2001
Posts: 48
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

Joined: Aug 21, 2005
Posts: 6
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Kevin Vicy
Greenhorn

Joined: Aug 21, 2005
Posts: 6
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Nov 29, 2001
Posts: 48
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Aug 21, 2005
Posts: 6
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
Peter Laurinec
Ranch Hand

Joined: Sep 13, 2004
Posts: 52
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


SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
 
 
subject: Hibernate(newbie) - generating relational database from Object model
 
Similar Threads
Java Data Objects - Chapter 1
Complete database newb any help appreciated:
JNDI error message
Model question
Your favorite ORM Tool and Why?