Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views14 pages

Hibernate

The document provides a comprehensive guide on using Maven and Hibernate for project management and development. It covers creating Maven projects, adding dependencies, and implementing CRUD operations with Hibernate, including using the Criteria API for querying. Additionally, it discusses inheritance, associations, and the use of annotations in Hibernate applications.

Uploaded by

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

Hibernate

The document provides a comprehensive guide on using Maven and Hibernate for project management and development. It covers creating Maven projects, adding dependencies, and implementing CRUD operations with Hibernate, including using the Criteria API for querying. Additionally, it discusses inheritance, associations, and the use of annotations in Hibernate applications.

Uploaded by

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

Date:18/Jan/2023

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

Maven Tool

----------

- Maven is a powerful project management tool

- Maven is used to build projects

- Maven is used to add the project dependency jar files into the project

- Maven is used to create the right project structure

- Maven is used to build and deploy the project

Creating a Maven Project

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

- Click on File -> New -> Maven Project

Click Next

In Catalog Select : Internal

In Artifact Id Select : maven-archetype-quickstart

Click Next

Group Id: maven

Artifact Id : FirstMavenProj (Project Name)

Package : com.maven

Click Finish
pom.xml

-------

- pom stands for project object model

- <project> is the root element of pom.xml file

- In pom.xml file we add the project dependency jar files

Adding mysql-connector-java-8.0.31.jar file into the project

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

Google: mysql connector jar maven

Copy the below dependency code and paste in between <dependencies> element

of pom.xml file

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.31</version>

</dependency>

Refer program pom.xml

Framework

---------

A framework is a group of predefined libraries which comes with

set of jar files


Hibernate Framework

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

- Hibernate is an ORM framework

- ORM stands for Object Relational Mapping

Refer diagram ORM.png

Hibernate Overview

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

- Hibernate is an ORM solution for Java and it raised as an open

source persistent framework created by Gavin King

- Hibernate maps Java classes to database tables and Java class propertites

to table columns using Hibernate XML mapping file

Ex:

emp.hbm.xml

- Hibernate sits between Java objects and database

Java Objects <-> Hibernate <-> Database

- Provides simple APIs for storing and retrieving Java objects

directly to and from the database

- save() => Insert command

- update() => Update command

- delete() => Delete command

- get()/list() => Select command


- In Hibernate we create Hibernate XML configuration file in which

we configure the database connection properties like driver class,

url, username and password and even in this file we refer to

Hibernate XML Mapping file

Ex:

hibernate.cfg.xml

- If there is a change in the database then the only need to change

is XML file properties

- Provides simple querying of data using Criteria API

Date:19/Jan/2023
----------------

Developing a Hibernate Application in Eclipse using Maven

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

- Create a Maven Project

Click on File -> New -> Maven Project

Click Next

In Catalog Select : Internal

In Artifact Id Select : maven-archetype-quickstart

Click Next

Group Id : hibernate

Artifact Id : HibernateProj

Package : com.hibernate

click Finish

- Add the following dependencies in between <dependencies> element of

pom.xml

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.3.11.Final</version>

</dependency>

<dependency>
<groupId>com.mysql</groupId>

<artifactId>mysql-connector-j</artifactId>

<version>8.0.31</version>

</dependency>

Refer program pom.xml

- Create a POJO Class "Student" in com.hibernate package of src/main/java folder

of HibernateProj

Refer program Student.java

- Create Hibernate XML Mapping file "student.hbm.xml" in src/main/java folder

Refer program student.hbm.xml

- Create Hibernate XML configuration file "hibernate.cfg.xml" in src/main/java folder

Refer program hibernate.cfg.xml

- Create a test class "StudentInsert" in com.hibernate package

Refer program StudentInsert.java (Run)

create vs update values in <property name="hbm2ddl.auto">

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

create

------

- drop existing tables

- create new tables


update

------

- use existing tables

- if table does not exist, create new tables

CRUD Operations

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

C - Create - Insert command - StudentInsert.java

R - Read - Select command - StudentList.java

U - Update - Update command - StudentUpdate.java

D - Delete - Delete command - StudentDelete.java


Date:20/Jan/2023

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

Hibernate Criteria API

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

Criteria API allows to build up a criteria query object programmtically

where we can apply filtration rules

Creating Criteria

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

Criteria cr = s.createCriteria(Student.class);
List<Student> studList = cr.list();

for(Student stud : studList)

SOP(stud);

Restrictions with Criteria

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

We can add restrictions to Criteria using Restrictions class

Ex:

Criteria cr = s.createCriteria(Student.class);

cr.add(Restrictions.gt("marks",600.0));//marks > 600

cr.add(Restrictions.ge("marks",600.0));//marks >= 600

cr.add(Restrictions.lt("marks",600.0));//marks < 600

cr.add(Restrictions.le("marks",600.0));//marks <= 600

cr.add(Restrictions.eq("marks",600.0));//marks = 600

cr.add(Restrictions.ne("marks",600.0));//marks != 600

like()

------

wildcards

---------

% => multiple characters

_ => single character

Display students whose name starts with "R"

cr.add(Restrictions.like("sname","R%"));
Display students whose name ends with "u"

cr.add(Restrictions.like("sname","%u"));

Display students whose name third char is "m"

cr.add(Restrictions.like("sname","__m%"));//two underscores

Display students whose name contains exact four characters

cr.add(Restrictions.like("sname","____"));//four underscores

Display students whose name contains a char "a"

cr.add(Restrictions.like("sname","%a%"));

Sorting

-------

The Criteria API provides Order class to sort the results

Ex:

Criteria cr = s.createCriteria(Student.class);

cr.addOrder(Order.asc("marks"));//ascending order

cr.addOrder(Order.dessc("marks"));//descending order

Refer program CriteriaDemo.java

Inheritance in Hibernate

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

<joined-subclass> element is used in Hibernate XML mapping file

for inheritance in Hibernate


Example Application

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

- Create a POJO class "Book" in com.hibernate package

Refer program Book.java

- Create a POJO class "AnnEditionBook" in com.hibernate package

Refer program AnnEditionBook.java

- Create Hibernate XML mapping file "book.hbm.xml" in src/main/java folder

Refer program book.hbm.xml

- Update hibernate.cfg.xml file by adding the following resource

<mapping resource="book.hbm.xml"/>

Refer program hibernate.cfg.xml

- Create a test class "BookTest" in com.hibernate package

Refer program BookTest.java (Run)

Associations in Hibernate

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

- Association means has-a relationship

- <one-to-many> element is used in Hibernate XML mapping file

for associations in Hibernate

Example Application
-------------------

- Create a POJO Class "Employee" in com.hibernate package

Refer program Employee.java

- Create a POJO Class "Department" in com.hibernate package

Refer program Department.java

- Create Hibernate XML mapping file "dept.hbm.xml" in src/main/java folder

Refer program dept.hbm.xml

- Update hibernate.cfg.xml file by adding the following mapping resource

<mapping resource="dept.hbm.xml"/>

Refer program hibernate.cfg.xml

- Create a test class "DeptTest" in com.hibernate package

Refer program DeptTest.java (Run)

Annotations in Hibernate

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

When Annotations are used in Hibernate we need not create

Hibernate XML mapping file

Example Application

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

- Create a POJO class "Item" in com.hibernate package

Refer program Item.java


- Update hibernate.cfg.xml file by adding the following mapping class

<mapping class="com.hibernate.Item"/>

- Create a test class "ItemTest" in com.hibernate package

Refer program ItemTest.java (Run)

You might also like