Your SlideShare is downloading. ×
0
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Hibernate Session 1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Hibernate Session 1

1,038

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,038
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
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. Hibernate Introduction Session - I
  • 2. Object Relational Mapping <ul><li>ORM refers to the technique of peristing objects in relational database using metadata that describes mapping between objects and database tables. </li></ul><ul><li>Performs automatic dirty checking, lazy associations etc.., </li></ul><ul><li>Has a language or API for specifying queries that refer to classes and properties. </li></ul>
  • 3. Hibernate <ul><li>Hibernate is a complete ORM tool. </li></ul><ul><li>1. Integrates elegantly with all popular Java EE application servers , web containers and also works in standalone applications . </li></ul><ul><li>2. Natural programming model – Support for Inheritance, polymorphism, composition and the Java collections framework </li></ul><ul><li>3. Scalability - has a dual-layer cache architecture </li></ul><ul><li>4. Unique query language which supports writing query on objects - HQL </li></ul>
  • 4. Contd.., <ul><li>5. Productivity – Eliminates much of Persistence-related code. </li></ul><ul><li>6. Vendor Independence </li></ul><ul><li>7. Solves portability issues </li></ul>
  • 5. Hibernate Object <ul><li>Hibernate object is a simple POJO with some recommended good practices as below: </li></ul><ul><li>There must be a default constructor for the class. </li></ul><ul><li>There must be accessors and optional mutators for all the instance variables of the class. (Immutable objcets are exceptional cases). </li></ul><ul><li>Recommended - POJO implements Serializable. </li></ul>
  • 6. Hibernate Object (contd..,) <ul><li>Recommended - POJO should ideally have an ID instance variable, usually of type Long. </li></ul><ul><li>Recommended - The mutator for the id property should be private, not public. </li></ul>
  • 7. Hibernate Object (contd..,) <ul><li>You should decide on a business key for the object and implement the equals and hashCode methods for it. </li></ul><ul><li>You should add any extra type specific constructors (which should leave the id field null) and business rule methods you like. </li></ul><ul><li>You should not make the class final if you want to be able to use lazy loading for objects of the class. </li></ul>
  • 8. Hibernate Architecture terminology <ul><li>SessionFactory </li></ul><ul><li>A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider. </li></ul><ul><li>Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level. </li></ul><ul><li>Session </li></ul><ul><li>A single-threaded, short-lived object representing a conversation between the application and the persistent store. </li></ul><ul><li>Wraps a JDBC connection. Factory for Transaction. </li></ul><ul><li>Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier. </li></ul>
  • 9. Database Interaction Pattern <ul><li>1. Create a Configuration object, load the configuration parameters from the hibernate.properties and adjust them as required. </li></ul><ul><li>2. Create a SessionFactory object from the Configuration object. The SessionFactory object is a heavyweight, thread safe object. You would normally share one such object between all your threads in a web application. </li></ul><ul><li>3. For each unit of work (normally one use case) use the SessionFactory object to obtain a Session object. This is an extremely lightweight, non-thread safe object. It will be associated with a database connection but it only obtains that connection lazily, i.e., only when (and if) it is required. Session objects must not be shared between different threads. </li></ul><ul><li>4. Inside a try block, get a Transaction object by calling beginTransaction() on the Session object. </li></ul>
  • 10. Hibernate - Database Interaction Pattern (contd..,) <ul><li>5. Interact with the database: </li></ul><ul><li>explicitly by calling methods of Session to associate objects to the database (i.e., map them to the database), execute queries, load, save, delete mapped objects etc. </li></ul><ul><li>implicitly by calling property mutators on mapped objects that will lead to the database being updated. </li></ul><ul><li>implicitly by referencing non-mapped objects from mapped objects which (in certain circumstances) can cause the non-mapped objects to be added to the database. </li></ul><ul><li>implicitly by unreferencing mapped objects from other mapped objects which (in certain circumstances) can cause the unreferenced objects to be deleted from the database. </li></ul><ul><li>6. Call commit() on the Transaction object and close the try block, handling exceptions and closing the Session object in the usual way. </li></ul>
  • 11. Hibernate Object - Life Cycle
  • 12. Hibernate Object - Life Cycle <ul><li>Transient objects DO NOT (yet) have any association with the database.They act like any normal Java object and are not saved to the database. </li></ul><ul><li>Persistent objects DO have an association with the database. </li></ul><ul><li>Detached objects are objects that were persistent but no longer have a connection to a Session object (usually because you have closed the session). A detached object may be re-attached later to another Session object to become persistent again. </li></ul>
  • 13. Built-in Mapping Types – Primitive Types
  • 14. Built-in Mapping Types – Date and Time Types
  • 15. Session I - Demo

×