0% found this document useful (0 votes)
21 views24 pages

Unit 4

The document discusses Spring and Hibernate frameworks. Spring is a Java framework that simplifies development while Hibernate is an ORM tool. The document covers the basics, architecture, configuration and integration of Spring and Hibernate.

Uploaded by

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

Unit 4

The document discusses Spring and Hibernate frameworks. Spring is a Java framework that simplifies development while Hibernate is an ORM tool. The document covers the basics, architecture, configuration and integration of Spring and Hibernate.

Uploaded by

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

19I602 Web Technologies Unit

4
DR.N.HEMA PRIYA
STAFF INCHARGE

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Agenda
Introduction to Spring and Hibernate
Fundamentals
Architecture
References
Spring is one of the most used Java EE Framework and Hibernate is
the most popular ORM framework. That’s why Spring
Hibernate combination is used a lot in enterprise applications.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Spring
The Spring Framework proposes to simplify and improve J2EE development

http://makble.com/what-is-spring-framework

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Spring (Contd..)

Its Core module provides a lightweight container for ordinary JavaBeans implements inversion of control
(IoC) and dependency injection, allowing complex graphs of objects to be declared rather than managed
programmatically.
 It simplifies exception handling by a combination of strategies:
 providing an excellent error reporting framework.
 It offers a flexible web framework that implements the model/view/controller pattern and offers pluggable
strategies for all of its high-level decision-making.
 It implements aspect-oriented programming (AOP) at many levels, allowing “cross-cutting concerns” to be
addressed effectively for even very complex applications.
 It offers a simple means of integrating persistence tools while preserving application-level control of
transactions.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Oct 17, 2023 DEPARTMENT OF IT, PSGCT
Spring Bean Scopes
There are five types of spring bean scopes:

1.singleton – only one instance of the spring bean will be created for the spring container. This is the
default spring bean scope. While using this scope, make sure bean doesn’t have shared instance
variables otherwise it might lead to data inconsistency issues.
2.prototype – A new instance will be created every time the bean is requested from the spring
container.
3.request – This is same as prototype scope, however it’s meant to be used for web applications. A
new instance of the bean will be created for each HTTP request.
4.session – A new bean will be created for each HTTP session by the container.
5.global-session – This is used to create global session beans for Portlet applications.

https://www.journaldev.com/21039/spring-bean-scopes

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Hibernate
• Hibernate is an open source project whose purpose is to make it easy to integrate relational data into Java
programs. Hibernate is an Object-Relational Mapping (ORM) solution for JAVA
• This is done through the use of XML mapping files, which associate Java classes with database tables.
• Hibernate provides basic mapping capabilities. It also includes several other object/relational mapping (ORM)
capabilities, including:
− An enhanced, object-based SQL variant for retrieving data, known as Hibernate Query Language (HQL).
− Automated processes to synchronize objects with their database equivalents. − Built-in database connection
pooling, including three opensource variants.
− Transactional capabilities that can work both stand-alone or with existing Java Transaction API (JTA)
implementations.
• The goal of Hibernate is to allow object-oriented developers to incorporate persistence into their programs
with a minimum of effort.
Oct 17, 2023 DEPARTMENT OF IT, PSGCT
Architecture

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Oct 17, 2023 DEPARTMENT OF IT, PSGCT
Contd..
Configuration Object
•Database Connection − This is handled through one or more configuration files
supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
•Class Mapping Setup − This component creates the connection between the Java
classes and database tables.
SessionFactory Object
Session Object
Transaction Object:
Query Object
Criteria Object

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Configure Hibernate
◦ http://www.hibernate.org/downloads

Installing Hibernate
Once you downloaded and unzipped the latest version of the Hibernate Installation
file, you need to perform following two simple steps. Make sure you are setting your
CLASSPATH variable properly otherwise you will face problem while compiling
your application.
•Now, copy all the library files from /lib into your CLASSPATH, and change your
classpath variable to include all the JARs −
•Finally, copy hibernate3.jar file into your CLASSPATH. This file lies in the root
directory of the installation and is the primary JAR that Hibernate needs to do its
work.
Oct 17, 2023 DEPARTMENT OF IT, PSGCT
Supporting Technologies

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Hibernate mapping types

It can translate from Java to SQL data types and vice versa.
The Map interface provides three collection views, which allow a map's contents to be
viewed as a set of keys, collection of values, or set of key-value mappings. A Map is
mapped with a <map> element in the mapping table and an unordered map can be
initialized with java. util.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Hibernate ORM (or simply Hibernate) is an object–relational mapping tool for the Java
programming language.
It provides a framework for mapping an object-oriented domain model to a
relational database.

Hibernate takes care of mapping Java classes to database tables using


XML files and without writing any line of code. Provides simple APIs for
storing and retrieving Java objects directly to and from the database. If there is
change in the database or in any table, then you need to change the XML file
properties only.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Hibernate - Annotations
Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational
Table mapping. All the metadata is clubbed into the POJO java file along with the code, this helps the
user to understand the table structure and POJO simultaneously during the development.
If you going to make your application portable to other EJB 3 compliant ORM applications, you must
use annotations to represent the mapping information, but still if you want greater flexibility, then you
should go with XML-based mappings.
Hibernate 3.x annotations distribution package

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Contd..
@Entity annotation to the Employee class, which marks this class as an entity bean

@Table annotation allows to specify the details of the table that will be used to persist
the entity in the database.
@Id annotation will automatically determine the most appropriate primary key
generation strategy to be used
@GeneratedValue annotation, which takes two parameters strategy and generator
The @Column annotation is used to specify the details of the column to which a field or
property will be mapped.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


https://www.tutorialspoint.com/hibernate/hibernate_annotations.htm

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Hibernate - Query Language
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but
instead of operating on tables and columns,
HQL works with persistent objects and their properties. HQL queries are translated by
Hibernate into conventional SQL queries, which in turns perform action on database.
Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like
table and column names are case sensitive in HQL.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Advantages
• Open Source and Lightweight. Hibernate framework is open source under the LGPL
license and lightweight.
• Fast Performance. ...
• Database Independent Query. ...
• Automatic Table Creation. ...
• Simplifies Complex Join. ...
• Provides Query Statistics and Database Status.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Spring and Hibernate
Despite their conflicting seasonal allusions, these two frameworks can cooperate neatly within the persistence tier.
• Hibernate can handle persistence more or less by itself.
− It offers object/relational mapping.
− It can manage sessions and transactions.
• Spring brings a few nice features not found in Hibernate:
− The IoC container makes configuring data sources, transaction managers, and DAOs easy.
− It manages the Hibernate SessionFactory as a singleton
– a small but surprisingly annoying task that must be implemented manually when using Hibernate alone.
− It offers a transaction system of its own, which is aspectoriented and thus configurable, either through Spring AOP or Java-
5 annotations. Either of these are generally much easier than working with Hibernate’s transaction API.
• As good as Hibernate is, Spring makes it a bit better.
− Transaction management becomes nearly invisible for many applications, and where it’s visible, it’s still pretty easy.
− You integrate more easily with other standards and frameworks.
Oct 17, 2023 DEPARTMENT OF IT, PSGCT
Configuring Hibernate Using Spring
A typical Hibernate application configures its SessionFactory using a properties file or an XML file.
• First, we start treating that session factory as a Spring bean.
− We declare it as a Spring and instantiate it using a Spring ApplicationContext.
− We configure it using Spring s, and this removes the need for a hibernate.cfg.xml or hibernate.properties
file.
− Spring dependency injection
– and possibly autowiring
– make short work of this sort of configuration task.
• Hibernate object/relational mapping files are included as usual.

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Spring RESTful CRUD operations with
Hibernate

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


Tools/Technologies: Here is an overview of the steps required:

•Spring •Create a Spring RESTful service using Maven archetype


•Hibernate
•Maven •Add methods for GET, POST, DELETE in the service class
•MySQL database
•Eclipse •Create DAO layer
•Postman or CURL
•Deploy and Test

topjavatutorial.com/frameworks/spring/spring-rest/spring-restful-crud-operations-hibernate/

https://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-and-hibernate-
login-form-example/

Oct 17, 2023 DEPARTMENT OF IT, PSGCT


References
https://www.journaldev.com/3524/spring-hibernate-integration-example-tutorial
https://www.tutorialspoint.com/hibernate/hibernate_environment.htm
https://www.tutorialspoint.com/hibernate/hibernate_examples.htm
http://www.hibernate.org/downloads
https://www.dineshonjava.com/hibernate-3-on-baby-steps/

Oct 17, 2023 DEPARTMENT OF IT, PSGCT

You might also like