Generate hibernate classes annotation database eclipse
Here you will see how to generate hibernate annotated class from database table.
Tools needed:
- Eclipse ( We are using eclipse Kepler)
- Install JBoss tools in eclipse. If you are not sure how to install JBoss in eclipse please user this URL:Install JBoss tools in eclipse
- MySQL data base (Install MySQL Community Server (GPL) version in your system : MySQL Community Server). We are using version 5.6 (Click here to see step by step installing instruction)
- Hibernate 3.2
- Maven 3.0.4
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:
- 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
- This will open Hibernate Configuration perspective. Right click –> click Add Configuration
- Choose as below:
- Project : Browse and select project
- Database connection: Click New
- Choose MySQL — Click Next
- Next window specify driver and connection details for MySQL database. Sample shown below:
- Before click next you to complete one more step to add jar for MySQL JDBC driver. For this click New driver definition as below:
- 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)
- On JAR List window — Click Add JAR/Zip to add jar then click OK
- 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:
- 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:
- Click OK. Now you will see list of table in Hibernate perspective as below:
- Right click hibernate –> Edit configuration
- Choose Annotation radio button and click OK
- Now in hibernate view click Hibernate Code Generation Configuration as below:
- Choose Main configuration as below:
- Now click Exporter tab and choose annotations and other option as below and click Run button:
- Go to Package explorer and refresh project you will see class and configuration files got generated in folder same as below for two tables:
- 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