How to Generate Sql Statements and Hibernate Config Files from Java Objects
Hibernate is a an object/relational mapper between Java and relational databases. SQL is a structured query language used to interact with relational databases. Hibernate allows Java developers to interact with the database (persistence) without needing to write SQL directly.
Most tools start with a database and reverse engineer it to get source code. Java developers, on the other hand, would like to write their source code and have the database generated.
This article explains the steps to analyze your existing Java classes and generate SQL statements and hibernate mapping XML files using a tool.
[edit] Steps
- Write source code to create Java objects for your Domain model.
- Make sure that Sets are parametrized for relationships between objects.
- Compile your source code in Java classes.
- Build your classes into a JAR file.
- Upload your JAR file at http://www.javatosql.com and provide your email address
- Make sure to select the box to receive Hibernate files
- Wait for a few minutes and check your email for your JAR submission
- Download JavaToSql.zip from your email
- Extract the ZIP file over your source code directory
- Run the sql queries against your database
[edit] Tips
- Java classes must be J2SE 5.0 (Major version number 49)
- All sets must be parametrized; lists are not allowed
- Each class must implement two methods:
- 1. public Integer getId()
- 2. public void setId(Integer id)
[edit] Warnings
- The tool Java to Sql is an alpha version
- The tool is not as customizable as you may need, so you may need to edit the sql statements before issuing them against your database
[edit] Sources and Citations
- Hibernate - http://www.hibernate.org
- Java to Sql - http://www.javatosql.com










