Generate hibernate classes annotation database eclipse

Generate hibernate classes annotation database eclipse

Here you will see how to generate hibernate annotated class from database table.

Tools needed:

Steps:

1. Create table in MySQL data base script:

CREATE TABLE address ( 
    Steet_Name  varchar(50) NULL,
    Location    varchar(25) NULL,
    City        varchar(25) NULL,
    Zip         varchar(25) NULL,
    State       varchar(25) NULL,
    id          int(11) NOT NULL DEFAULT '0' ,
    PRIMARY KEY (id)
    )
GO

CREATE TABLE person ( 
    First_Name  varchar(50) NULL,
    Last_Name   varchar(50) NULL,
    id          int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
    FOREIGN KEY (id)
    REFERENCES address (id) )

 

2. Eclipse setup:

  • Open your eclipse
  • Create Maven project name: HibernateOneToOneAnnotation (If you are not sure how to create Maven project in eclipse please click this tutorial)
  • By default eclipse kepler creates dynamic maven project of version 2.3. If you try to change version to 2.5 through Right click project –>Properties –> Project Facet will give you an error and if you are getting error then click Window –> Show view –> Other –> General –> Navigator –> Click OK
  • Eclipse will change in Navigator perspective. Click .setting folder — and open file name Open org.eclipse.wst.common.project.facet.core.xml file then make change show below:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <fixed facet="wst.jsdt.web"/>
  <installed facet="java" version="1.5"/>
  <installed facet="jst.web" version="2.5"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

 

  • This will change maven dynamic project version 2.5.
  • Now create resources folder inside src/main.
  • Create package com.javahonk and com.javahonk.bean and com.javahonk.util
  • Project structure shown below:

Generate hibernate classes annotation database eclipse

  • Below is dependency needed to generate annotation. Please copy past below dependency in your pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javahonk</groupId>
    <artifactId>HibernateOneToOneAnnotation</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>HibernateOneToOneAnnotation Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        
        <!-- MySQL database -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.3.ga</version>
        </dependency>


        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>HibernateOneToOneAnnotation</finalName>
    </build>
</project>

Hibernate configuration:

  • Click Window –> Show View –> Others –> Choose Hibernate –> Hibernate Configuration

Generate hibernate model class and mapping files automatically

  • This will open Hibernate Configuration perspective. Right click –> click Add Configuration
  • Choose as below:
    • Project : Browse and select project
    • Database connection: Click New

Generate hibernate model class and mapping files automatically

  • Choose MySQL — Click Next

Generate hibernate model class and mapping files automatically

  • Next window specify driver and connection details for MySQL database. Sample shown below:

Generate hibernate model class and mapping files automatically

  • Before click next you to complete one more step to add jar for MySQL JDBC driver. For this click New driver definition as below:

Generate hibernate model class and mapping files automatically

  • Choose MySQL version 5.1 then click JAR List and add mysql-connector-java-5.1.0-bin.jar (This jar is already include in project so you can download it from bottom)

Generate hibernate model class and mapping files automatically

  • On JAR List window — Click Add JAR/Zip to add jar then click OK

Generate hibernate model class and mapping files automatically

  • Please test connection before click next. To test connection click Test Connection. If everything configured correctly it will show you ping successful window as below:

Generate hibernate model class and mapping files automatically

  • Now on Edit launch configuration properties window on Configuration file box –> Click set up button to create new or use existing file as sample below:

Generate hibernate model class and mapping files automaticallyGenerate hibernate model class and mapping files automatically

  • Click OK. Now you will see list of table in Hibernate perspective as below:

Generate hibernate classes annotation database eclipse

  • Right click hibernate –> Edit  configuration

Generate hibernate classes annotation database eclipse

  • Choose Annotation radio button and click OK

Generate hibernate classes annotation database eclipse

  • Now in hibernate view click Hibernate Code Generation Configuration as below:

Generate hibernate classes annotation database eclipse

  • Choose Main configuration as below:

Generate hibernate classes annotation database eclipse

  • Now click Exporter tab and choose annotations and other option as below and click Run button:

Generate hibernate classes annotation database eclipse

  • Go to Package explorer and refresh project you will see class and configuration files got generated in folder same as below for two tables:

Generate hibernate classes annotation database eclipse

  • Now for test open Address.java class you will annotation added in the class:
package com.javahonk.bean;

// Generated May 20, 2014 10:09:34 PM by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * Address generated by hbm2java
 */
@Entity
@Table(name = "address", catalog = "javahonk")
public class Address implements java.io.Serializable {

    private int id;
    private String steetName;
    private String location;
    private String city;
    private String zip;
    private String state;
    private Person person;

    public Address() {
    }

    public Address(int id) {
    this.id = id;
    }

    public Address(int id, String steetName, String location, String city, String zip,
        String state, Person person) {
    this.id = id;
    this.steetName = steetName;
    this.location = location;
    this.city = city;
    this.zip = zip;
    this.state = state;
    this.person = person;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
    return this.id;
    }

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

    @Column(name = "Steet_Name", length = 50)
    public String getSteetName() {
    return this.steetName;
    }

    public void setSteetName(String steetName) {
    this.steetName = steetName;
    }

    @Column(name = "Location", length = 25)
    public String getLocation() {
    return this.location;
    }

    public void setLocation(String location) {
    this.location = location;
    }

    @Column(name = "City", length = 25)
    public String getCity() {
    return this.city;
    }

    public void setCity(String city) {
    this.city = city;
    }

    @Column(name = "Zip", length = 25)
    public String getZip() {
    return this.zip;
    }

    public void setZip(String zip) {
    this.zip = zip;
    }

    @Column(name = "State", length = 25)
    public String getState() {
    return this.state;
    }

    public void setState(String state) {
    this.state = state;
    }

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "address")
    public Person getPerson() {
    return this.person;
    }

    public void setPerson(Person person) {
    this.person = person;
    }

}

That’s it Generate hibernate classes annotation database eclipse

Leave a Reply

Your email address will not be published. Required fields are marked *