Hibernate with Spring MVC Integration
Techno Kryon |
Hibernate:
Hibernate ORM is
an object oriented-relational mapping tool for
the Java programming language. It provides a framework for
mapping an object-oriented model to a relational database Management System
(RDBS). Hibernate
handles object-relational domain mismatch problems by replacing persistent database
accesses with high-level object handling functions and direct replacement.
Spring MVC:
The Spring Web MVC framework provides Model-View-Controller
(MVC) architecture and ready components that can be used to develop loosely
coupled and flexible web applications.
Spring MVC simple concept like Dispatcher
Servlet, ModelAndView and ViewResolver, It makes easy to develop Web Applications.
It is similar to struts framework.
Specify each and every dependency
separately. We need to a lot of
configuration to be written to achieve the same. But the spring boot was
packaged as a jar with an embedded server by default.
The most popular elements are classes
annotated with @Controller. When the user sends the request it is accepted by a
Dispatcher Servlet.
Hibernate With
Spring:
Hibernates
primary feature is mapping from Java data types to SQL data types and
mapping from Java classes to database tables. Hibernate also provides data
query and retrieval facilities for further Process. Hibernate generates SQL
calls and object conversion of the result set and relieves the developer from
the manual handling.
Hibernate (HQL) like to create
Configuration, Session, BuildSessionFactory, beginning and committing
transaction etc. Hibernate used to data manipulation works (insert, select,
update and delete) in MySQL database Using spring MVC. Hibernate Session Factory
provides three methods through which we can get Session object –openSession(), getCurrentSession()
and openStatelessSession().
Important Dependencies for Spring and Hibernate Integration:
Spring-context and spring-tx for
core spring functionalities.
Spring dependency for Spring ORM
support, it’s required for hibernate integration in our spring project.
Hibernate-entity manager and
hibernate-core dependencies for the Hibernate framework. For using Hibernate 4,
we need to change it to 4.3.5. Final as commented in above pom.xml file.
Mysql-connector-java for MySQL
driver for a database connection.
There are
4 types of Mapping Present in Hibernate that can be further divided into
bidirectional and unidirectional mappings
One-to-One Mapping
One-to-Many Mapping
Many-to-One Mapping
Many-to-Many Mapping
One-to-One Mapping:
For example, you can create two tables (Primary
and secondary table). Both of these
tables will have a unidirectional mapping. Primary table and we will be using Foreign Key in
the Secondary table for one-to-one mapping (one row in a table can be mapped to
one row in another table.).
One-to-Many
Mapping &Many-to-One Mapping:
For example, you
can create two tables (Primary and secondary table). Both of these tables will
have one to many mapping. Primary table has only one value and secondary table
have a collection of values to hold (one row in a table can be mapped to one row
(multiple values) in another table.).
Many-to-Many Mapping:
Many-to-Many mapping is
usually implemented in the database by Join Table. Many-to-Many map used to map
many row values. You can create two tables (Primary and secondary table) for
Example. Both of these tables will have many to many mapping.
Advantage For Hibernate:
Hibernate is much easier to maintain via Spring MVC
facilities.
Spring Exception translation.
Easily Configuring Hibernate in a spring context.
Spring control dependencies between our
objects.
Comments
Post a Comment