Download presentation
Presentation is loading. Please wait.
Published byLinette Jennings Modified over 8 years ago
1
Hibernate Java Persistence API
2
What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence using Relational Databases. Relational Databases are based on the theoretical foundation of the relational data model.
3
Object Oriented Persistence Object Oriented Languages like Java and C# need to persist Objects, not Tuples. To achieve this programmers would need to write a DAO layer where objects would be persisted in the DB with the help of a DB API such as JDBC or ADO.NET
4
Problems with OO to Relational Mapping Granularity ◦ Refers to the relative size of types we are working with ◦ Domain models support coarse-grained, fine grained & simple types ◦ SQL supports 2 level, tables and columns Subtypes ◦ Object oriented languages support inheritance and polymorphism ◦ Relational databases do not have this concept.
5
Problems with OO to Relational Mapping Identity ◦ Object equality is checked by either “==“ or the equals method ◦ Database row identity is expressed with the primary key Associations ◦ Object associations are directional with references ◦ Table associations are non-directional with foreign keys
6
Object/Relational Mapping(ORM) An ORM aims to solve the Object Relational impedance mismatch. The ORM provides a set of API that enable access to persistent Relational data in an Object Oriented Manner.
7
JPA 2.0 JPA stands for Java Persistence API The JPA specification details the Interfaces and Annotations that Persistence providers must adhere to. The current discussion revolves around JPA 2.0 (JSR 317)
8
JPA 2.0 JPA aims to provides transparent persistence, allowing the application to switch between database vendors or even persistence mechanism Provides complete separation of concerns between domain business logic and persistence
9
Hibernate Hibernate 3.x is an ORM that conforms to JPA 2.0 Hibernate existed well before JPA 2.0 became a specification, as a result there are Hibernate specific classes and annotations as well as JPA specific Where ever possible, JPA classes and annotations are advised since it’s an abstraction over Hibernate allowing the application to change persistence providers.
10
Entities v/s Objects Entities are POJOs that are annotated with @Entity, thus identifying them as persistable Entities are required to have a field serve as Database Identity. Entities are Granular – they are not primitive wrappers or built in types, neither are they as coarse-grained as having 500 attributes.
11
Core Hibernate Interfaces
12
Session An instance of Session represents a single unit-of-work for a given data store. A session wraps a JDBC connection. The instances are light weighted and can be created and destroyed without expensive process. All interactions with the database are through Session objects. The Session acts as the first level cache.
13
SessionFactory As its name implies it is used to create session objects The SessionFactory is an expensive object to create. It is usually created during application start up. Generally, an application has single SessionFactory and can be shared by all the application threads. The SessionFactory can also provide caching of persistent objects.
14
Configuration It represents a configuration or properties file for Hibernate. The Configuration object is usually created once during application initialization. The Configuration object reads and establishes the properties Hibernate uses to get connected to a database and configure itself for work. A Configuration object is used to create a SessionFactory and then typically is discarded.
15
JPA Equivalents HibernateJPA 2.0 SessionFactoryEntityManagerFactory Configured by a Persistence Unit SessionEntityManager Manages a Persistence Context Configuration (org.hibernate.cfg. Configuration) Persistence (javax.persistencePersistence)
16
Dialects SQL dialects are just like natural language dialects. Databases implement subtle differences in the SQL they use. ◦ Things such as data types for example vary across databases (e.g. Oracle NUMBER and SQL Server uses int ) ◦ Or database specific functionality - selecting the top n rows is different depending on the database. The dialect abstracts this so you don't have to worry about it.
17
References Java Persistence with Hibernate ◦ Bauer, King (Manning) Pro JPA 2 ◦ Keith, Schincariol(Apress) Hibernate Reference Documentation ◦ http://docs.jboss.org/hibernate/core/3.6/refere nce/en-US/ http://docs.jboss.org/hibernate/core/3.6/refere nce/en-US/ JSR 317 – JPA 2.0 Specification ◦ http://jcp.org/en/jsr/detail?id=317 http://jcp.org/en/jsr/detail?id=317
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.