• Like
Hibernate An Introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Hibernate An Introduction

  • 437 views
Published

This presentation gives an introduction about Hibernate which covers fundamental concepts such as POJOs, Data Model, Persistent Lifecycle, HQL, etc.

This presentation gives an introduction about Hibernate which covers fundamental concepts such as POJOs, Data Model, Persistent Lifecycle, HQL, etc.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
437
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
14
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Basic Fundamentals Hibernate: An Introduction Cao Duc Nguyen nguyen.cao-duc@hp.com Software Designer HP Software Products and Solution May 3, 2012 Conclusion
  • 2. Basic Fundamentals Talk Outline 1 Basic Getting Started Problem Challenges 2 Fundamentals Concepts Data Model Persistent Lifecycle HQL Architecture 3 Conclusion Conclusion
  • 3. Basic Fundamentals Getting Started Setting up the Environment JDK Ant/Maven MySQL/PostgreSQL/Oracle, etc. JDBC Eclipse Hibernate Tools Conclusion
  • 4. Basic Getting Started Hibernate ORM distribution Fundamentals Conclusion
  • 5. Basic Fundamentals Conclusion Problem Hibernate - Definitions by Hibernate.org Hibernate is concerned with helping your application to achieve persistence. . . Persistence simply means that we would like our application’s data to outlive the applications process. . . would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later. by Wikipedia.org Hibernate is an object-relational mapping (ORM) library for the Java language, . . . a framework for mapping an object-oriented domain model to a traditional relational database . . .
  • 6. Basic Problem Traditional View Fundamentals Conclusion
  • 7. Basic Problem Traditional View (cont.) Fundamentals Conclusion
  • 8. Basic Problem Hibernate View Fundamentals Conclusion
  • 9. Basic Fundamentals Challenges Two Different Worlds Object-Oriented Systems System composed of objects interacting with each other Objects encapsulate data and behaviors Relational Databases Data is stored in tables composed of rows Conclusion
  • 10. Basic Fundamentals Challenges Obstacles Identity Granularity Associations Navigation Inheritance & Polymorphism Data type mismatches Conclusion
  • 11. Basic Fundamentals Conclusion Concepts Plain Old Java Object (POJOs) In general, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification. However, due to technical difficulties and other reasons, in the context of Hibernate, a POJO is defined as follow: No-argument class constructor Property accessor (get/set) methods Class is not declared final nor has final methods. Collection-typed attributes must be declared as interface types.
  • 12. Basic Fundamentals Conclusion Data Model Example Application: EventApp Event-management application used to manage a conference with speakers, attendees, and various locations, among other things.
  • 13. Basic Data Model An Example: EventApp Fundamentals Conclusion
  • 14. Basic Fundamentals Conclusion Data Model Identity Mapping <id name="id" column="id" type="long"> <generator class="native"/> </id> Mapped classes must declare the primary key column of the database table. Generators using the native class will use identity or sequence columns depending on available database support. If neither method is supported, the native generator falls back to a high/low generator method to create unique primary key values. The native generator returns a short, integer, or long value. Hibernate documentation about Identity mapping here.
  • 15. Basic Fundamentals Conclusion Data Model Property Mapping <property name="startDate" column="start_date" type="date"/> A typical Hibernate Property mapping defines a POJO property name, a database column name, and the name of a Hibernate type, and it is often possible to omit the type. Hibernate uses reflection to determine the Java type of the property. Details about Hibernate Types mapping here. Hibernate documentation about Property mapping here.
  • 16. Basic Fundamentals Conclusion Data Model Entity Mapping <class name="Event" table="events"> <!-- define identity, properties, components, collections, associations here... --> </class> A typical Hibernate Entity mapping defines a POJO class name, a database table name. By default, all class names are automatically “imported” into the namespace of HQL Hibernate documentation about Entity mapping here.
  • 17. Basic Fundamentals Conclusion Data Model Component Mapping <component name="componentName" class="componentClass"> <!-- defines properties of the component here these properties will be mapped to columns of the enclosing entity--> </class> A Hibernate Component mapping is defined within an Entity mapping several objects into one single table of the enclosing entity. Hibernate documentation about Component mapping here.
  • 18. Basic Fundamentals Data Model Put it all together Hibernate Mapping files: The Event.hbm.xml mapping file Conclusion
  • 19. Basic Fundamentals Data Model Put it all together (cont.) The Location.hbm.xml mapping file Conclusion
  • 20. Basic Fundamentals Data Model Put it all together (cont.) Hibernate Configuration hibernate.cfg.xml file: Conclusion
  • 21. Basic Fundamentals Conclusion Data Model Collection Mapping Common Collections: sets, lists, bags, maps of value types. Value Type: An object of value type has no database identity; it belongs to an entity instance, and its persistent state is embedded in the table row of the owning entity Value-typed classes do not have identifiers or identifier properties
  • 22. Basic Fundamentals Data Model Collection Mapping (cont.) Hibernate persistent collections Conclusion
  • 23. Basic Fundamentals Data Model Collection Mapping (cont.) Example of persisting collections Conclusion
  • 24. Basic Fundamentals Data Model Inheritance Mapping Table per concrete class with union: Conclusion
  • 25. Basic Fundamentals Data Model Inheritance Mapping (cont.) Table per class hierarchy: Conclusion
  • 26. Basic Fundamentals Data Model Inheritance Mapping (cont.) Table per subclass: Conclusion
  • 27. Basic Fundamentals Conclusion Persistent Lifecycle Object States Transient The object is not associated with any persistence context. It has no persistent identity (primary key value). Persistent The object is currently associated with a persistence context. It has a persistent identity (primary key value) and, perhaps, a corresponding row in the database. Hibernate guarantees that persistent identity is equivalent to Java identity.
  • 28. Basic Fundamentals Conclusion Persistent Lifecycle Object States (cont.) Detached The instance was once associated with a persistence context, but that context was closed, or the instance was serialized to another process. It has a persistent identity and, perhaps, a corrsponding row in the database. For detached instances, Hibernate makes no guarantees about the relationship between persistent identity and Java identity
  • 29. Basic Persistent Lifecycle State Transition Diagram Fundamentals Conclusion
  • 30. Basic Fundamentals Conclusion Persistent Lifecycle Case 1: Making an object persistent Item item = new Item(); item.setName("Playstation3 incl. all accessories"); item.setEndDate( ... ); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Serializable itemId = session.save(item); tx.commit(); session.close();
  • 31. Basic Fundamentals Conclusion Persistent Lifecycle Case 2: Retrieving a persistent object Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item item = (Item) session.load(Item.class, new Long(1234)); // Item item = (Item) session.get(Item.class, // new Long(1234)); tx.commit(); session.close();
  • 32. Basic Fundamentals Conclusion Persistent Lifecycle Case 3: Modifying a persistent object Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item item = (Item) session.get(Item.class, new Long(1234)); item.setDescription("This Playstation is as good as new!"); tx.commit(); session.close();
  • 33. Basic Fundamentals Conclusion Persistent Lifecycle Case 4: Making a persistent object transient Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item item = (Item) session.load(Item.class, new Long(1234)); session.delete(item); tx.commit(); session.close();
  • 34. Basic Fundamentals Conclusion Persistent Lifecycle Case 5: Reattaching a modified detached instance // Loaded in previous Session item.setDescription(...); Session sessionTwo = sessionFactory.openSession(); Transaction tx = sessionTwo.beginTransaction(); sessionTwo.update(item); item.setEndDate(...); tx.commit(); sessionTwo.close();
  • 35. Basic Fundamentals HQL Query HQL Query: HQL SQLQuery: Conclusion
  • 36. Basic HQL Parameter Binding Fundamentals Conclusion
  • 37. Basic Fundamentals HQL Joins Join: Left Join: Conclusion
  • 38. Basic Fundamentals Conclusion HQL Criteria API Some developers prefer to build queries dynamically, using an object-oriented API, rather than building query strings. Hibernate provides an intuitive org.hibernate.Criteria represents a query against a particular persistent class:
  • 39. Basic Fundamentals HQL DetachedCriteria A DetachedCriteria is used to express a subquery. Conclusion
  • 40. Basic Fundamentals Architecture Structural Components More in depth explanation can be found here. Conclusion
  • 41. Basic Fundamentals Architecture Hibernate Flexibility and Extendibility Extension points: Dialects (for different databases) Custom mapping types ID generators Cache, CacheProvider Transaction, TransactionFactory PropertyAccessor ProxyFactory ConnectionProvider Conclusion
  • 42. Basic Fundamentals Why Hibernate? Free, open source Java package Release developers from data persistent related tasks, help to focus on objects and features of application No need for JDBC API for Result handling Database almost-independence Efficient queries Conclusion
  • 43. Basic Fundamentals THANK YOU *-* Conclusion