How To Generate POJOs and Mapping File (Hibernate)

First of all, create new project which is use hibernate on the project. Then create database and table that you will use for the project. If you use ant, then you can add hibernate lib which is available from your IDE. But if you use maven, then add hibernate libs on your POM file. Mine is like this:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.4-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.4-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>3.2.4.GA</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>            
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

Right click on your source package then choose to create new hibernate configuration file.

new hibernate config

hb2

I already setup configuration to my MYSQL so I only need to choose the configuration. If you don’t have any database configuration, then create new on and select it. Click finish
hb3

hb4

After that, add hibernate reverse engineer.

hb5

hb6

hb7

hb8

Right click on your source model package then choose hibernate POJOs and Mapping file.

hb9

hb10

hb11

Here is my result.

package com.aris.springmvchib.model;
// Generated Nov 7, 2014 7:01:53 PM by Hibernate Tools 3.6.0

import java.util.HashSet;
import java.util.Set;

/**
 * User generated by hbm2java
 */
public class User  implements java.io.Serializable {

     private Integer id;
     private String name;
     private String username;
     private String password;
     private int enable;
     private Set<UserRoles> userRoleses = new HashSet<UserRoles>(0);

    public User() {
    }

    public User(String name, String username, String password, int enable) {
        this.name = name;
        this.username = username;
        this.password = password;
        this.enable = enable;
    }
    public User(String name, String username, String password, int enable, Set<UserRoles> userRoleses) {
       this.name = name;
       this.username = username;
       this.password = password;
       this.enable = enable;
       this.userRoleses = userRoleses;
    }

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

    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public int getEnable() {
        return this.enable;
    }

    public void setEnable(int enable) {
        this.enable = enable;
    }
    public Set<UserRoles> getUserRoleses() {
        return this.userRoleses;
    }

    public void setUserRoleses(Set<UserRoles> userRoleses) {
        this.userRoleses = userRoleses;
    }
}

package com.aris.springmvchib.model;
// Generated Nov 7, 2014 7:01:53 PM by Hibernate Tools 3.6.0

/**
 * UserRoles generated by hbm2java
 */
public class UserRoles  implements java.io.Serializable {

     private Integer userRoleId;
     private User user;
     private String role;

    public UserRoles() {
    }

    public UserRoles(User user, String role) {
       this.user = user;
       this.role = role;
    }

    public Integer getUserRoleId() {
        return this.userRoleId;
    }

    public void setUserRoleId(Integer userRoleId) {
        this.userRoleId = userRoleId;
    }
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public String getRole() {
        return this.role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 7, 2014 7:01:54 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="com.aris.springmvchib.model.User" table="user" catalog="learnhibernate">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="Name" length="50" not-null="true" />
        </property>
        <property name="username" type="string">
            <column name="Username" length="50" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="Password" length="100" not-null="true" />
        </property>
        <property name="enable" type="int">
            <column name="Enable" not-null="true" />
        </property>
        <set name="userRoleses" table="user_roles" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="user_id" not-null="true" />
            </key>
            <one-to-many class="com.aris.springmvchib.model.UserRoles" />
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 7, 2014 7:01:54 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="com.aris.springmvchib.model.UserRoles" table="user_roles" catalog="learnhibernate">
        <id name="userRoleId" type="java.lang.Integer">
            <column name="user_role_id" />
            <generator class="identity" />
        </id>
        <many-to-one name="user" class="com.aris.springmvchib.model.User" fetch="select">
            <column name="user_id" not-null="true" />
        </many-to-one>
        <property name="role" type="string">
            <column name="ROLE" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Hope this can help.. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s