File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes My hibernate generator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "My hibernate generator" Watch "My hibernate generator" New topic
Author

My hibernate generator

Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

I wrote my own HBM generator that can be used by everybody else. So if you use MSSQL or MYSQL you can find this generator usefull.

This ANT script is a generator for Hibernate mapping files. Generator can create a JAR file that contains set of generated HBM and class files. One HBM file and one class file are generated for every mapped table. You can select subset of the table, provide exact Java types mapping of every supported SQL type, select hibernate version, choose a primary key generator. After JAR file is generated you can add it to your project in order to connect your project to the database using Hibernate framework. After you add Jar file to your project you have to change your hibernate configuration file and add the references to generated HBM files. For your convenience, text file that has list of mapped files is also generated.

hbm4stas
[ May 03, 2006: Message edited by: Stas Sakalou ]

Good luck for yourself.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Um, why not just use XDoclet to generate your files? If you use Hibernate 3.0 just use Annotations.

Curious, I might be missing something here. Or are you generating the .java and xml from the database, which Hibernate has tools that do this too. I'm lost, sorry.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Um, why not just use XDoclet to generate your files? If you use Hibernate 3.0 just use Annotations.

Curious, I might be missing something here. Or are you generating the .java and xml from the database, which Hibernate has tools that do this too. I'm lost, sorry.

Mark

I dont know what to say :-)
At the time I started writing this stuff it was the only tool available that can scan DB and create HBM files for every found table. It was a middlegen. But middlegen is always(!) generating relationships between tables if both tables have fields with the same name. For example member.name and provider.name. These fields might have different meaning but middlegen still goes ahead with generation relationship.

Because I needed some tool that can go trough the DB and create me mapping files I wrote this tool myself.

Probably now there is something else that does this work better...
Just let me know where I can find this new tool. Thank you.
[ May 04, 2006: Message edited by: Stas Sakalou ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OK, I did a Google search for middlegen and saw that it uses XDoclet. Here is XDoclet's web site.

Hibernate also has a generation tool.

Mark
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

XDoclet and Hibernate annotations are reverse mapping tools.
They map java classes to HBM files. That has a little practical usage for the heterogeneous projects.

Java->HBM mapping makes sense for the projects that are completely driven by Java code. What is not the case in the big distributed projects where single DB might be used by applications written using different languages. In this case DB->HBM->Java DTO mapping is required to easily update DTO objects after DB was changed. Let take an example.
Imagine that DB has 400 tables. Many of these tables were changed due to new requirements. Now, it is required to regenerate DTO in order to take latest changes made by DB admin and also replace class that is used as PK generator.
My generator allows you to do this in 2-3 minutes... Probably there are other tools that are doing the same job but it looks for me that Hibernate Annotations and xDoclet are not in this list.

Middlgen can do dirrect DB->HBM mapping but, as I didnt said, using velocity templates. Unfortunatly at the time I was using it, Middlegen generator was very unflexable in its configuration: Relationship mapping could not be switched off for example, primary key generator class could not be configured for individual tables and some other restrictions were implied.

Eclispse plugins
There is a beta-version of some "eclipse-only-working" project that supports reverse engeenering approach. It does about the same job as my generator does and it also allows to map relationship that I currently do not support.

[ May 04, 2006: Message edited by: Stas Sakalou ]

[ May 04, 2006: Message edited by: Stas Sakalou ]

[ May 04, 2006: Message edited by: Stas Sakalou ]

[ May 04, 2006: Message edited by: Stas Sakalou ]

[ May 04, 2006: Message edited by: Stas Sakalou ]

[ May 04, 2006: Message edited by: Stas Sakalou ]
[ May 04, 2006: Message edited by: Stas Sakalou ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes, Hibernate has a tool that takes the DB stucture and creates mapping file and then java code. That is what I am saying that you can go any direction you want with Hibernate tools that already exist. The XDoclet was pre-Annotations to allow you to keep the mapping and Java in one file.

Mark
 
 
subject: My hibernate generator
 
Similar Threads
[Hibernate] many-to-many, invalid primary key in the join table?
issue with Xdocler Ant Task
Help with HQL
Querying tables with a one-to-many foreign key relationship
MySQL to Oracle