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

Generate Hibernate mappings from DB

 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a simple question, but I can't seem to find the answer anywhere. I have an existing database (in MySQL). I want to generate the hbm.xml files from it. I imagine that's quite easy to do, but how do you actually go about it?

It's the opposite of a SchemaExport. Still, I imagine this is a huge area for Hibernate, since most apps already have a db schema in place.

Thanks,

Ken
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Middlegen.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like a good approach. The Middlegen info says it only works for Hibernate 2.1, though. Is that vastly different from version 3.0? Is an updated middlegen plugin on the way?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're using Middlegen 2.1 to create mapping files for Hibernate 3.0.5 with no issues. After Middlegen creates the files, we change the DTD from:



to:



We generally tweak the files anyway so this additional change isn't a problem.

Middlegen saves us a lot of grunt work. We have a complex schema with many associations. We tried writing the mappings by hand but it was very tedious and error-prone. With Middlegen, we generate the mappings, make a few changes and we're done in minutes.

A new version of Middlegen is in development that supports Hibernate 3 specific mappings, but we don't need any so Middlegen 2.1, with the above change, works fine for us.
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the assistance. I think Middlegen is the answer, but (I hate to admit this) I can't quite see how to run it to generate the Hibernate files. How do you set up the ant task to do the job?

(I know, RTFM, but it's still not obvious to me.)

Ken
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run Middlegen using Ant. There's an example Ant target near the top of this page.

Farther down, there is a target for hbm2java that you can use to generate class files from the mapping files.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Middlegen is a piece of s..t. I spent a week trying to force it to dont generate dependencies. No way. It considers that relations between tables should always be reflected in the mapping. Middlegen uses velocity templates to generate hbm.xml. I found them difficult to understand. Eventually I wrote generator myself spending 4 hours. It scan db and create hbm.xml files with no relatioship mapping.
 
Sayama Ahmed
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stas ,

can you please tell me about the tool that you wrote to generate mappings.

Thanks and Regards,
 
asus asus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I have my ant script doing sql->hbm->classes thing.
I'm new to it and i used examples form Internet.
It works.
The Big problem is to rewrite it to maven script.

I have mysql db. I need two goals: one generating hbm files and second, using generated hbms and generating classes.
I jus can't cope with it myself, so please send me examples if u have with project.xml, *.properties and everything what is needed. I really stuck in one point and i need help.
 
Pj Murray
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kenneth A. Kousen:
This may be a simple question, but I can't seem to find the answer anywhere. I have an existing database (in MySQL). I want to generate the hbm.xml files from it. I imagine that's quite easy to do, but how do you actually go about it?


CodeFutures has just announced GA for FireStorm/DAO 3.0. The major new feature is importing existing database schemas (from a SQL script or from a live JDBC connection) and can then generate a complete persistence tier based on Hibernate.

If I see you request an evaluation license key, I'll foward a full license key ;-)
 
pranith rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kenneth A. Kousen:
This may be a simple question, but I can't seem to find the answer anywhere. I have an existing database (in MySQL). I want to generate the hbm.xml files from it. I imagine that's quite easy to do, but how do you actually go about it?

It's the opposite of a SchemaExport. Still, I imagine this is a huge area for Hibernate, since most apps already have a db schema in place.

Thanks,

Ken


Hi,

I use hibernate synchroniser. this tool even generates DAOs.

pranith
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"asus asus"

Perhaps you haven't returned to your last post, but there you were asked to change your display name inline with JavaRanch's naming policy. You can change it here.

Thank you.
[ December 12, 2005: Message edited by: Paul Sturrock ]
 
Mohd. Yakub
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

One more plugin for eclipse3.2 ,jdk 1.5 & hibernate 3.0 that generate hbm as well as class files ,no issues.It is from sourceforge..try
net.sourceforge.sqlexplorer_2.2.4
Cheers,
yakub
[ December 12, 2005: Message edited by: Mohd. Yakub ]
 
pradeep jaladi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can you reply back how to use the sql explorer to generate the hbm files. I gone through the http://www.sqlexplorer.org/ site but did not find any support for hbm file generation.
 
Rama Krishna
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We really need another place/forum where we have users discuss/know about their experiences/matching for these specific applications. I am looking for a similar application and I was thinking Hibernate-Tools is the way to go for everything.

Pranith, When you say,
I use hibernate synchroniser. this tool even generates DAOs.

Could you explain about the DAO's thing a little detailed please.

Stas Sakalou
Any take on the other plugins/tools that can do a better job. I have no idea what velocity templates are though!

I am looking for something similar too where the mapping files are generated and the mapping includes all fields and relationship to all related tables as well and I can always remove the unnecessary table mappings (I am not sure why I will remove them though!) whenever I do not need them.

So I am confused what to start using as I do not have time to test or research on what is the currently the best way to do it.

-Cheers
Rama
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic