0% found this document useful (0 votes)
143 views

Hibernate Tool

The document describes the steps to use the Hibernate reverse engineering tool to generate domain classes, mapping files, and configuration files from an existing database table. It provides instructions on adding the Hibernate plugin to Eclipse, generating a configuration file, generating a mapping file from a database table, and includes an example of generating and saving an Employee domain object to demonstrate the process.

Uploaded by

Arobinda Bachhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

Hibernate Tool

The document describes the steps to use the Hibernate reverse engineering tool to generate domain classes, mapping files, and configuration files from an existing database table. It provides instructions on adding the Hibernate plugin to Eclipse, generating a configuration file, generating a mapping file from a database table, and includes an example of generating and saving an Employee domain object to demonstrate the process.

Uploaded by

Arobinda Bachhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Hibernate Tool or

Hibernate Reverse Engineering Tool


➔ Reverse Engineering:
Any thing opposite of Regular activity is called as Reverse
Engineering Tool.
➔ Hibernate Reverse Engineering Tool:
If you have a table this reverse Engineering tool will generate
Domain classes, Mapping files, Configuration files if needed DAO
classes automatically and dynamically this is called as Hibernate
Reverse Engineering Tool.

Note: If you are using annotations-based Domain classes then no


need of mapping files.

Adding JBOSS(Hibernate tool) plugin in Eclipse:

Step1:
Help->Eclipse Marketplace
Step2:
Find text box there type-> jboss ->go
Step3:
Jboss tools->install
Step4:
De-select All
Step5:

Select Hibernate Tool and Confirm


Step6:

Accept Terms and Conditions Finish


Step7:

Install anyway
Step8:

Restart now
Step9:

Open Perspective
Step10:

Confirm successfully installed or not hibernate tool


See hibernate option in perspective
plugin adding completed

Creating Configuration File :


Note: if you are Created one time Configuration file you can use in
multiple projects.

Step1: change perspective to Hibernate

Step2:
Go to file……
Step3:
Step4:
Step5:
Step6:

Select type of Driver press ok


Step7:

Next tab JAR List add ojdbc6.jar then ok

Step8:
Test connection
Step9:
next
Step10:

Finish
Step11:
Step12:
Step13:
Step14:
Step15:
Step16:

Select dialect
Step17:

Finish
Step18:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</pr
operty>
<property name="hibernate.connection.password">tiger</property>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORC
L</property>
<property
name="hibernate.connection.username">scott</property>
<property
name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prope
rty>
</session-factory>
</hibernate-configuration>

Configuration file generation Completed.

Mapping File Generation


Step1:

Step2:
Step3:
Step4:
Step5:
Step6:
Step7:
Step8:
Step9:

Wait few Minutes

Step10:

Select user
Step11:

Here Select table and include


Step12:

Select table finish


Step13:

Go to Exports tab
Step14:
Step15:

If any extra configurations coming delete it


Step16:

Mapping file code:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generated 02-Feb-2021, 1:36:59 pm by Hibernate Tools
5.4.27.Final --><!DOCTYPE hibernate-mapping PUBLIC "-
//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property"
default-cascade="none" default-lazy="true">
<class dynamic-insert="false" dynamic-update="false" mutable="true"
name="in.nit.domain.Employee" optimistic-lock="version"
polymorphism="implicit" schema="SCOTT" select-before-update="false"
table="EMPLOYEE">
<composite-id class="in.nit.domain.EmployeeId" mapped="false"
name="id" unsaved-value="undefined">
<key-property name="id" type="java.lang.Long">
<column name="ID" precision="10" scale="0"/>
</key-property>
<key-property name="name" type="string">
<column length="30" name="NAME"/>
</key-property>
</composite-id>
</class>
</hibernate-mapping>

Mapping file generation completed

Note: Here you can select domain class, mapping file, dao class
Etc……………..
Sample Example:

Step in pom.xml file:

<properties>
<project.build.sourceEncoding>UTF-
8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--
https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.20.Final</version>
</dependency>
<!--
https://mvnrepository.com/artifact/com.jslsolucoes/ojdbc6 -->
<dependency>
<groupId>com.jslsolucoes</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>

</dependencies>

Step2:

Step3: Create all Required packages:


Step4:

Configuration file generated

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</pr
operty>
<property name="hibernate.connection.password">tiger</property>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORC
L</property>
<property
name="hibernate.connection.username">scott</property>
<property
name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prope
rty>
<mapping class="in.nit.domain.Employee"></mapping>
</session-factory>
</hibernate-configuration>

Step5:

Domain class generated


Note : if any extra classes and any configurations coming delete it.

package in.nit.domain;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee implements java.io.Serializable {
@Id
private Integer id;
private String name;

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;
}

Step6:

Dao class:

Interface:

package in.nit.dao;

import in.nit.domain.Employee;

public interface EmployeeDao {


public int save(Employee emp);
}

Impl:
package in.nit.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import in.nit.domain.Employee;

public class EmployeeDaoImpl implements EmployeeDao {

@Override
public int save(Employee emp) {
Transaction tx=null;
int id=0;
//create hibernate SessionFactory, Session Objects
Session ses=new
Configuration().configure("/in/nit/cfgs/hibernate.cfg.xml").buildSessi
onFactory().openSession();
tx=ses.beginTransaction();

//Save Object
try {
id=(Integer)ses.save(emp);
tx.commit();
}
catch(Exception e) {
tx.rollback();
e.printStackTrace();
}
return id;
}

Factory:

package in.nit.dao;

public class EmployeeDaoFactory {


public static EmployeeDao getInstane() {

return new EmployeeDaoImpl();


}
}

Step7:

SaveTest

package in.nit.test;

import in.nit.dao.EmployeeDao;
import in.nit.dao.EmployeeDaoFactory;
import in.nit.domain.Employee;

public class SaveTest {

public static void main(String[] args) {


EmployeeDao dao=null;
Employee emp=null;
//get dao
dao=EmployeeDaoFactory.getInstane();
//invoke method
emp=new Employee();
emp.setId(2344478);
emp.setName("Sankar");
int eno=dao.save(emp);
System.out.println("Record is Saved in Database"
+eno);
}

Output is:

Record is Saved in Database 34445


-------------------The End------------------------------------------------

You might also like