0% found this document useful (0 votes)
23 views11 pages

HIBERNET-day12 and 13

The document outlines the integration of Maven with Eclipse for managing dependencies in a Java project, specifically focusing on Hibernate as an ORM tool. It details the creation of a Maven project, the configuration of Hibernate with XML and annotations, and provides code examples for managing a 'Department' entity. Additionally, it includes instructions for setting up the Hibernate configuration file and mapping files for database interactions.

Uploaded by

rethinakumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

HIBERNET-day12 and 13

The document outlines the integration of Maven with Eclipse for managing dependencies in a Java project, specifically focusing on Hibernate as an ORM tool. It details the creation of a Maven project, the configuration of Hibernate with XML and annotations, and provides code examples for managing a 'Department' entity. Additionally, it includes instructions for setting up the Hibernate configuration file and mapping files for database interactions.

Uploaded by

rethinakumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Day12:

======

Eclipse -

Maven Integration

Repository - https://mvnrepository.com/

Central/Remote/Local

Auto - Build tool

Project Object Model => POM.xml

Proj info

group id

artifact id

version

Dependencies

dependency

group id

artifact id

version

ex:
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc6 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>

if not reflected, Maven Dependencies folder,

Right click - project

-Maven

-Update Project

-select force update

-Ok

or else check BuildPath - Libraries - Maven Dependencies

Frameworks
ORM tools - Object Relational Mapping tool

Object ----- Relation

class-----------Table

Hibernate

ORM tool

Configuration - xml

hibernate-configuration hibernate-
config.xml

driver/db/usr/pwd/

mapping

Session

Mapping xml hbm

class ---- table

var col

Hib Annotation

class ---- tb

var-----col

HQL

Hibernate Query Language

Object - Employee

eid,name,sal

class Employee{

private int eid;

private String name;

private int sal;

//getters/setters

Table: EmpTB

columns

--------
EID | NAME | SAL

==================

…..

JDBC API Hibernate

----------- ------------

addEmp(Employee e) configuration

1.Load driver save(e)

2.Establish Connection

3.Prepare/Create qry statement

4.Execute statement

5.Process Result

6.Close connection

updtEmp(Employee e)

1.Load driver

2.Establish Connection

3.Prepare/Create qry statement update()

4.Execute statement

5.Process Result

6.Close connection

………

Maven Repository: Search/Browse/Explore

Day13:

======

Hibernate

Create new maven project

Simple

group id : com.wipro

artifact id : DeptHibApp

open pom.xml
with in project tag, copy this dependencies
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.9.Final</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
</dependencies>

Save the pom.xml

Maven Dependencies folder added in project explorer

expand and confirm the jar files

Right click on src/main/java

new class

package: com.wipro.bean

class: Department

Department.java

-----------------

package com.wipro.bean;

public class Department {

private int dno;

private String dname;

private String loc;

public Department() {

super();

public Department(int dno, String dname, String loc) {

super();

this.dno = dno;

this.dname = dname;

this.loc = loc;

}
public int getDno() {

return dno;

public void setDno(int dno) {

this.dno = dno;

public String getDname() {

return dname;

public void setDname(String dname) {

this.dname = dname;

public String getLoc() {

return loc;

public void setLoc(String loc) {

this.loc = loc;

@Override

public String toString() {

return "Department [dno=" + dno + ", dname=" + dname + ", loc=" + loc +
"]";

---

Right click o src/main/resources


new xml file
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name =
"hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name =
"hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- wcf20jan is the database name -->
<property name =
"hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name = "hibernate.connection.username">wcf20jan</property>
<property name = "hibernate.connection.password">wcf20jan</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource = "department.hbm.xml"/>
</session-factory>
</hibernate-configuration>
For MYSQL, use below for dialect
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
Right click on src/main/resources
new xml
department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.bean.Department" table="dept">
<id name="dno" column="deptno" type="int">
<generator class="assigned"></generator>
</id>
<property name="dname" column="dname" type="string"></property>
<property name="loc" column="loc" type="string"></property>
</class>
</hibernate-mapping>

Right click on src/main/java new class ... by Athma M


Athma M
1:20 PM
Right click on src/main/java
new class
package name: com.wipro.mainpkg
class : DepartmentMain

DepartmentMain.java
====================
package com.wipro.mainpkg;

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

import com.wipro.bean.Department;

public class DepartmentMain {

public static void main(String[] args) {


Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session =factory.openSession();
Transaction tn = session.beginTransaction();
Department d = session.get(Department.class, 100); // new
Department(100,"Sales","Bangalore");
System.out.println(d);
//session.save(d);
//System.out.println("Department details saved");
d.setLoc("Pune");
session.update(d);
System.out.println("Department details updated");
System.out.println(session.get(Department.class, 100));
tn.commit();
session.close();
factory.close();
}
}
----

Demo with Hibernate annotation: only chan... by Athma M


Athma M
1:20 PM
Demo with Hibernate annotation:
only change is in the hibernate.cfg file and Department.java file
hibermate.cfg.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name =
"hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name =
"hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- wcf20jan is the database name -->
<property name =
"hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name = "hibernate.connection.username">wcf20jan</property>
<property name = "hibernate.connection.password">wcf20jan</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<!-- List of XML mapping class -->
<mapping class="com.wipro.bean.Department"/>
</session-factory>
</hibernate-configuration>

Add annotation in Department.java


Department.java
===============
package com.wipro.bean;

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

@Entity
@Table(name="Department")
public class Department {
@Id
@Column(name="deptno")
private int dno;
@Column(name="dname")
private String dname;
@Column(name="loc")
private String loc;
public Department() {
super();
}
public Department(int dno, String dname, String loc) {
super();
this.dno = dno;
this.dname = dname;
this.loc = loc;
}
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Department [dno=" + dno + ", dname=" + dname + ", loc=" + loc +
"]";
}
}
--------------
Check the changes usingDepartmentMain.java
DepartmentMain.java
==================
package com.wipro.mainpkg;

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

import com.wipro.bean.Department;

public class DepartmentMain {

public static void main(String[] args) {


Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session =factory.openSession();
Transaction tn = session.beginTransaction();
Department d = new Department(200,"IT","Bangalore");
session.save(d);
System.out.println("Department details saved");
//Department d = session.get(Department.class, 100);
//System.out.println(d);
//d.setLoc("Pune");
//session.update(d);
//System.out.println("Department details updated");
System.out.println(session.get(Department.class, 200));
tn.commit();
session.close();
factory.close();
}
}

Tomorrow:

Spring

has context menu

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


<!DOCTYPE hibernate-configuration SYSTEM

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name =
"hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name =
"hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- wcf20jan is the database name -->
<property name =
"hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name = "hibernate.connection.username">wcf20jan</property>
<property name = "hibernate.connection.password">wcf20jan</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource = "department.hbm.xml"/>
</session-factory>
</hibernate-configuration>

For MYSQL, use below for dialect


<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.bean.Department" table="dept">
<id name="dno" column="deptno" type="int">
<generator class="assigned"></generator>
</id>
<property name="dname" column="dname" type="string"></property>
<property name="loc" column="loc" type="string"></property>
</class>
</hibernate-mapping>

DepartmentMain.java

package com.wipro.mainpkg;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.wipro.bean.Department;

public class DepartmentMain {

public static void main(String[] args) {

Configuration config = new Configuration();

config.configure("hibernate.cfg.xml");

SessionFactory factory = config.buildSessionFactory();

Session session =factory.openSession();

Transaction tn = session.beginTransaction();

Department d = new Department(100,"Sales","Bangalore");

session.save(d);

System.out.println("Department details saved");

tn.commit();

session.close();

factory.close();

}
}

You might also like