0% found this document useful (0 votes)
2 views7 pages

Hibernate Notes

The document discusses Hibernate, JPA, and ORM, highlighting their roles in simplifying database operations in Java applications. It outlines the drawbacks of JDBC, the specifications of JPA, and the functionalities of Hibernate as an ORM tool, including project setup and configuration. Additionally, it explains various mapping relationships and cascade operations in Hibernate, providing insights into managing entity states.

Uploaded by

nishchaygo88
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)
2 views7 pages

Hibernate Notes

The document discusses Hibernate, JPA, and ORM, highlighting their roles in simplifying database operations in Java applications. It outlines the drawbacks of JDBC, the specifications of JPA, and the functionalities of Hibernate as an ORM tool, including project setup and configuration. Additionally, it explains various mapping relationships and cascade operations in Hibernate, providing insights into managing entity states.

Uploaded by

nishchaygo88
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/ 7

Hibernate Notes

Hibernate
JDBC Drawbacks

SQL queries
Should follow five steps
Lengthy code
Creation of table and set the values
Mapping is much more difficult

JPA
1. JPA is a acronym of JAVA PERSISTENCE API.
2. JPA is a specification, not an implementation.
3. It is the standard application programming interface that makes database operations simple for
developers to carry out.
4. A JPA (Java Persistence API) is a specification of Java which is used to access, manage, and persist
data between Java object and relational database.
5. As JPA is a specification it does not perform any operations by itself, thus it requires implementation
so ORM tools to implements JPA specification for data persistence.
6. JPA specification is same for all the ORM tools and it follows same standards, so in the future if we
want to switch our application from one ORM tool to another then we can do it easily.

ORM
1. ORM is an acronym of OBJECT-RELATIONAL MAPPING.
2. ORM is a technique for converting data between java objects and relational database.
3. ORM Handles the logic required to interact with databases.
4. You write less code when using ORM tools than with JDBC.
5. There are some popular ORM tools:
Hibernate, Top Link, Eclipse Link , Open JPA
6. The most used ORM tool is Hibernate.
7. The ORM tool internally uses the JDBC API to interact with the database
8. ORM tool does mapping in such a way that model class become table in the db and each instance
will become a row of the table
HIBERNATE
1. Hibernate is a Java framework that simplifies the development of Java application to interact with
the database
2. Hibernate is a open-source, light-weight, ORM tool .
3. Hibernate implements JPA specification for data persistence.
4. Hibernate framework can be used to create tables in the database automatically.
5. It can be used to perform all the CRUD operations without having to write SQL queries.

Creation of Project
Create a maven project
Add mysql connector and hibernate core dependency
Add META-INF Folder inside the src/main/resource
Add xml file /file inside META –INF with persistence.xml name

Src/mai
n/resou
rce

META-INF

Persistence.xml
Persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="radha">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/hibernate_has_a?createDatabaseIfNotExist=true" />
<property name="javax.persistence.jdbc.user"
value="root" />
<property name="javax.persistence.jdbc.password"
value="root" />
<property name="hibernate.show_sql" value="true" />

<property name="hibernate.hbm2ddl.auto" value="update" />


<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>

</properties>
</persistence-unit>
</persistence>

Inbuilt methods :
persist()------to save
merge()--------(if not exist===save )/(if exist===update)
remove()-----to delete
find()-------to find based on id(primary key)

Query:
 It’s a interface
 Present in javax.persistence package
 Interface is used to control query execution.
 entityManager.createQuery(jpql); ------ create an instance of query to execute
 Used to create JPQL query and can call the object according to the query
 If the Query is wrong it throws a illegalArgumentexception
 It accepts only string format parameter.
 It provides many methods to retrieve the objects.
AutoGeneration:
• Want to automatically generate the primary key value,we can add the
@GeneratedValue(strategy=GenerationType.IDENTITY)
• Jpa And Hibernate support different strategies to generate the primary key values.one of
them is Identity Strategy
• Which uses on auto incremented database column
• The strategy are of four types. Identity,Auto,sequence,table
• Identity- database is responsible for determining and assignment the next primary key

Mapping(Has A RelationShip):
OwningClass:
 An Owning class is also referred to as the parent or master class or depending class
 The owning class has a foreign key column that references the primary key of the associated Class
NonOwningClass:
 While the associated class is referred to as the child or dependent class
1. unidirection(Should happen(persist,delete…..) in uni direction)
 One to one-one entity class(owning class) having another one non owning class
 One to many-One owning class have many non owning class
 Many to one-many owning class objects have same or one non owning class
 Many to many-many owning classes have many non owning classes
2. Bidirection(Should happen(persist,delete…..) in bi direction)
 One to one
 One to many /many to one
 Many to many

ONETOONE(UNI)
CASCADE
• Is a feature in hibernate ,which is used to manage the state of the mapped entity
whenever the state of its relationship owner affected.
• When the relationship owner is saved ,deleted……. Then the mapped entity
associated with it should also be saved , deleted….automatically.
• @OneToOne(cascade=CascadeType.PERSIST)
• CascadeTypes are :
1. Persist – used to save both entity object at a time
2. Merge – used to update both entity object at a time.
3. Detach – used to detach both entity object at a time.
4. Remove –used to delete both entity object at a time
5. Refresh– used to save both entity object at a time
6. All– used to perform all operations from both entity object at a time
One to one
I need to fetch the data from owning side ====both
I need to fetch the data from non owning side ====one

One to many
I need to fetch the data from owning side ===both
I need to fetch the data from non owning side ====one

Many to one
I need to fetch the data from owning side =====both
I need to fetch the data from non owning side ====one

Manytomany
I need to fetch the data from owning side =====both
I need to fetch the data from non owning side ====one

You might also like