Create DAO using hibernate in few clicks
Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities.
So here I'm going to describe how to generate POJO( Plain Old Java Object ) from database and corresponding DAO ( Data Access Object ) to manipulate/query tables programming way.
I'm using Eclipse Mars in this example.
Step 1 :
Install JBoss tool from Eclipse Market Place which has a Eclipse plugins for Hibernate Help->Eclipse Marketplace
Install JBoss tool from Eclipse Market Place which has a Eclipse plugins for Hibernate Help->Eclipse Marketplace
Step 2 : Create a Maven Project as shown
Select Artifact type as maven-archetype-quickstart then click Next
Enter GroupId ,artifact Id and package name then click Finish which will create a maven project in your work space.
Step 3 : Change windows perspective to hibernate windows->Perspective ->Open Perspective ->Others Select Hibernate
Step 4 :
Creating a hibernate configuration for the existing database.
File -> New -> Hibernate Configuration File then select the target Maven project created before click Next
Click on Next
Select the hibernate version (In this example I'm using Hibernate v4.0) and in order to set the database connection parameter click on Get values from Connection
You can select the existing connection if configured before other wise click on New and create a connection using below steps
Select the type of database (in this example I'm using MySQL following steps may be different for different types of database conection)
Select Driver version
Add/Browse mysql driver jar file (it is suggested to browse it from the maven repository ) click ok
Provide the connection information in the connection window and Test the connection to verify the configuration then click on Finish
Step 5 :
Verify the database schema in hibernate configuration window of eclipse
Step 6 :
Create Hibernate Code Generation Configuration as follows
Right click on Hibernate Code Configuration and click New and set up configuration like below
Setup Exporter configuration
Click on Run and verify the code generated in package explorer
Update pom.xml file with following dependencies
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.0.CR2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
Happy Coding :)
Comments
Post a Comment