Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Maven Project?

• Apache Maven is a software project management and comprehension


tool.
• Download and install maven in eclipse
• Google-download apache maven-download the zipfile-Binary Zip
archive
• Put the downloaded and extracted folder where the java folder is
present.
Updating the path

• Open Advanced System Settings-Edit System Variables-


• Variable name-path
• Click on edit put ;
• And paste the bin path after semicolon
• Org.apache.maven
• Cmd>mvn –version
• In eclipse goto wndows menu-preferences-maven-
• Check Download repository index updates on startup
What is JDBC?

• JDBC (Java Database Connectivity) provides a set of Java APIs to


access the relational databases from the Java program. Java APIs
enable programs to execute SQL statements and interact with any SQL
database.
• JDBC gives a flexible architecture to write a database-independent
web application that can execute on different platforms and interact
with different DBMS without any change.
ORM Tool
• An ORM tool simplifies the data creation, data manipulation and data
access and It is a programming technique that maps the object to the
data stored in the database.

The ORM tool internally uses the JDBC API to interact with the database.
What is JPA?

• Java Persistence API (JPA) is a Java specification that provides


specific functionality and is a standard for ORM tools.
• javax.persistence package contains the JPA classes and interfaces.
• JPA stands for Java Persistence API (Application Programming
Interface). It was initially released on 11 May 2006. It is a Java
specification that gives some functionality and standard to ORM tools.
It is used to examine, control, and persist data between Java objects
and relational databases.
Need of Hibernate Framework
• Hibernate is used to overcome the limitations of JDBC
• Hibernate is a framework which provides some abstraction layer,
meaning that the programmer does not have to worry about the
implementations, Hibernate does the implementations for you
internally like Establishing a connection with the database, writing
query to perform CRUD operations etc.
What is Hibernate?
• It is a java framework which is used to develop persistence logic.
Persistence logic means to store and process the data for long use.
More precisely Hibernate is an open-source, non-invasive, light-
weight java ORM(Object-relational mapping) framework to develop
objects which are independent of the database software and make
independent persistence logic in all JAVA, JEE.
• Framework means it is special install-able software that provides an
abstraction layer on one or more technologies like JDBC, Servlet, etc
to simplify or reduce the complexity for the development process.
• Hibernate is a ORM tool means it support Object relational mapping.
Whereas JDBC is not object oriented moreover we are dealing with
values means primitive data
Hibernate Architecture

• Hibernate is a framework which is used to develop persistence logic


which is independent of Database software. In JDBC to develop
persistence logic we deal with primitive types. Whereas Hibernate
framework we use Objects to develop persistence logic which are
independent of database software.
Class to Table mapping in Hibernate
Cfg file configuration

<?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="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost/16septhibernate</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">3</property>
<property name="show_sql">true</property>

<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<property
name="hibernate.current_session_context_class">thread</property
>

</session-factory>
Pom.xml

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
Users.java

import javax.persistence.Entity;
import javax.persistence.*;
@Entity
@Table(name="users")

class Users1 {

public Users1(String username, String password, String firstname, String lastname) {


super();
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
}
@Id
@Column(name="user_Id")
int userId;
@Column(name="username")
String username;
@Column(name="password")
String password;
@Column(name="first_name")
String firstname;
@Column(name="last_name")
String lastname;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}

}
App.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import javax.*;
public class App
{
public static void main( String[] args )
{
SessionFactory factory=new
Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Users1.class).buildS
essionFactory();
Session session =factory.getCurrentSession();
try
{
Users1 user=new Users1(“bharti123","2121","Bharti","Dutta");
session.beginTransaction();

session.save(user);
session.getTransaction().commit();

}
finally
{
session.close();
factory.close();
}
}
• Configuration:
• Configuration is a class which is present in org.hibernate.cfg package.
It activates Hibernate framework. It reads both configuration file and
mapping files.
SessionFactory:
• SessionFactory is an Interface which is present in org.hibernate
package and it is used to create Session Object.
• It is immutable and thread-safe in nature.
Session:
Session is an interface which is present in org.hibernate package.
Session object is created based upon SessionFactory object i.e. factory.
It opens the Connection/Session with Database software through
Hibernate Framework.
It is a light-weight object and it is not thread-safe.
Session object is used to perform CRUD operations.
Hibernate Lifecycle

• In Hibernate, we can either create a new object of an entity and store


it into the database, or we can fetch the existing data of an entity from
the database. These entity is connected with the lifecycle and each
object of entity passes through the various stages of the lifecycle.
• There are mainly four states of the Hibernate Lifecycle :
1.Transient State
2.Persistent State
3.Detached State
4.Removed State
State 1: Transient State

• The transient state is the first state of an entity object. When we


instantiate an object of a POJO class using the new operator then the
object is in the transient state. This object is not connected with any
hibernate session. As it is not connected to any Hibernate Session, So
this state is not connected to any database table. So, if we make any
changes in the data of the POJO Class then the database table is not
altered.
• Here, we are creating a new object for the Employee class. Below is
the code which shows the initialization of the Employee object :

//Here, The object arrives in the transient state.


Employee e = new Employee();
e.setId(21);
e.setFirstName("Neha");
e.setMiddleName("Shri");
e.setLastName("Rudra");
State 2: Persistent State

• Once the object is connected with the Hibernate Session then the
object moves into the Persistent State. In this state. each object
represents one row in the database table. Therefore, if we make any
changes in the data then hibernate will detect these changes and make
changes in the database table.
• Following are the methods given for the persistent state:
• session.persist(e);
• session.save(e);
• session.saveOrUpdate(e);
• session.update(e);
• session.merge(e);
• session.lock(e);
• Example:
// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
//Persistent State session.save(e);
State 3: Detached State

• For converting an object from Persistent State to Detached State, we either have to
close the session or we have to clear its cache. As the session is closed here or the
cache is cleared, then any changes made to the data will not affect the database table.
Whenever needed, the detached object can be reconnected to a new hibernate session.
To reconnect the detached object to a new hibernate session, we will use the following
methods as follows:
• merge()
• update()
• load()
• refresh()
• save()
• update()
• Following are the methods used for the detached state :
• session.detach(e);
• session.evict(e);
• session.clear();
• session.close();
• Example

// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
// Persistent State session.save(e);
// Detached State session.close();
State 4: Removed State

In the hibernate lifecycle it is the last state.


In the removed state, when the entity object is deleted from the database then the entity object is known to be in the removed state.
It is done by calling the delete() operation. As the entity object is in the removed state, if any change will be done in the data will not affect the database table.
Note: To make a removed entity object we will call session.delete().
// Java Pseudo code to Illustrate Remove State //
Transient State Employee e = new Employee();
Session s = sessionfactory.openSession();
e.setId(01);
// Persistent State
session.save(e)
// Removed State
session.delete(e);

You might also like