views:

1256

answers:

3

Are there any tools that auto-generate the hibernate POJOs by gathering information from the database?

I made a perl script to do this after the schema got changed for the third or fourth time in a project i'm working with and just wondered if there is any established tool that will do this for me as my script is rather crude and needs some tweaking of the generated .java and .hbm.xml files.

For example (based on what my script does), having a table statement:

Object_Name  Type_Name Base_Type Nulls Identity
------------ --------- --------- ----- --------
id           id_int    int       no    yes
id_user      id_tiny   tinyint   no    no
value        float     float     no    no

I would like the program to autogenerate the following java:

package com.some.company.schema;

import java.io.Serializable;

public class Statement implements Serializable {
    private static final long serialVersionUID = 1227171190L;
    private Integer id;
    private User user;
    private Float value;

    public void setId(Integer id){
        this.id = id;
    }

    public Integer getId(){
        return this.id;
    }

    public void setUser(User user){
        this.user = user;
    }

    public User getUser(){
        return this.user;
    }

    public void setValue(Float value){
        this.value = value;
    }

    public Float getValue(){
        return this.value;
    }
}

And the corresponding Hibernate mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping>
    <class name="com.some.company.schema.Statement" table="statement">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <many-to-one name="user" class="com.some.company.schema.User" column="id_user" not-null="true" />
        <property name="value" column="value" not-null="true" />
    </class>
</hibernate-mapping>
A: 

See hibernate tools

http://www.hibernate.org/hib_docs/tools/reference/en/html_single/

Not sure about from the schema to Pojos but definitely from hbm.xml files to pojos.

Maven plugins also available that make automated builds a dream Paul

Paul Whelan
+1  A: 

Netbeans does a nice job of generating annotations entities that will work with hibernate-annotations/entitmanager. I've also used the Hibernate Tools/JBoss IDE in Eclipse to generate them and it worked pretty well.

jsight
+2  A: 

See hibernate reverse engineering

LenW
looks interesting, thanks
dsm
pleasure - give me a shout if u need more help
LenW