Sunday, February 3, 2013

Generating POJOs from Database Scema (Used HSQL)

Ok, Now in a project we have the tables created in a database server. How can i leverage those tables to generate my hibernate pojos or domain objects easily?

    
          a. Install Hibernate Tools.
          b. Go to Hibernate Perspective (Window -> Open Perspective -> Other -> Hibernate)
          c. In the Hibernate Configurations View, Right click -> Add Configuration
          d. Under Main Tab,
                * Select the project where the POJOS need to be generated.
                * Select the database connection as Hibernate Configured Connection.
                * Create the hibernate Configuration file (Properties file is not mandatory) with the following values.
                    <?xml version="1.0" encoding="UTF-8"?>
                    <!DOCTYPE hibernate-configuration PUBLIC
                            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
                    <hibernate-configuration>
                        <session-factory name="TechFes">
                            <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
                            <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:9001/TechFes</property>
                            <property name="hibernate.connection.username">SA</property>
                            <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
                        </session-factory>
                    </hibernate-configuration>
          e. Now go to Run icon (the run icon in Hibernate perspective) and click on Hibernate Code Generation Configurations.
          f. Under Main Tab,
                * Select the output directory where the files has to get generated.
                * Select the "Reverse Engineer from JDBC Connection" checkbox.
                            * Provide the package name under which the domain java objects need to be created.
                            * Uncheck "Detect many-to-many" associations. This will help in generating the pojo objects for the tables that are maintained only for many-many association.
          g. Under Exporters Tab,
                * Check the Domain Code, hibernate cfg file and if required annotations, etc.
               
          h. Click on Run and your domain code should have got generated.

No comments:

Post a Comment