KEMBAR78
Hibernate Session 1 | PPT
Hibernate Introduction Session - I
Object Relational Mapping ORM refers to the technique of peristing objects in relational database using metadata that describes mapping between objects and database tables. Performs automatic dirty checking, lazy associations etc.., Has a language or API for specifying queries that refer to classes and properties.
Hibernate Hibernate is a complete ORM tool. 1. Integrates elegantly with all popular Java EE application servers , web containers and also works in standalone applications . 2. Natural programming model – Support for Inheritance, polymorphism, composition and the Java collections framework 3. Scalability - has a dual-layer cache architecture 4. Unique query language which supports writing query on objects -  HQL
Contd.., 5. Productivity – Eliminates much of Persistence-related code. 6. Vendor Independence  7. Solves portability issues
Hibernate Object Hibernate object is a simple POJO with some recommended good practices as below: There must be a default constructor for the class. There must be accessors and optional mutators for all the instance variables of the class. (Immutable objcets are exceptional cases). Recommended - POJO implements Serializable.
Hibernate Object (contd..,) Recommended - POJO should ideally have an ID instance variable, usually of type Long.  Recommended - The mutator for the id property should be private, not public.
Hibernate Object (contd..,) You should decide on a business key for the object and implement the equals and hashCode methods for it. You should add any extra type specific constructors (which should leave the id field null) and business rule methods you like. You should not make the class final if you want to be able to use lazy loading for objects of the class.
Hibernate Architecture terminology SessionFactory   A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider.  Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level.  Session A single-threaded, short-lived object representing a conversation between the application and the persistent store.  Wraps a JDBC connection. Factory for Transaction.  Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
Database Interaction Pattern 1.   Create a Configuration object, load the configuration parameters from the hibernate.properties and adjust them as required. 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. 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.  4.  Inside a try block, get a Transaction object by calling beginTransaction() on the Session object.
Hibernate - Database Interaction Pattern (contd..,) 5. Interact with the database: 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. implicitly by calling property mutators on mapped objects that will lead to the database being updated. 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. implicitly by unreferencing mapped objects from other mapped objects which (in certain circumstances) can cause the unreferenced objects to be deleted from the database.  6.  Call commit() on the Transaction object and close the try block, handling exceptions and closing the Session object in the usual way.
Hibernate Object - Life Cycle
Hibernate Object - Life Cycle 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. Persistent objects  DO  have an association with the database.  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.
Built-in Mapping Types – Primitive Types
Built-in Mapping Types –  Date and Time Types
Session I - Demo

Hibernate Session 1

  • 1.
  • 2.
    Object Relational MappingORM refers to the technique of peristing objects in relational database using metadata that describes mapping between objects and database tables. Performs automatic dirty checking, lazy associations etc.., Has a language or API for specifying queries that refer to classes and properties.
  • 3.
    Hibernate Hibernate isa complete ORM tool. 1. Integrates elegantly with all popular Java EE application servers , web containers and also works in standalone applications . 2. Natural programming model – Support for Inheritance, polymorphism, composition and the Java collections framework 3. Scalability - has a dual-layer cache architecture 4. Unique query language which supports writing query on objects - HQL
  • 4.
    Contd.., 5. Productivity– Eliminates much of Persistence-related code. 6. Vendor Independence 7. Solves portability issues
  • 5.
    Hibernate Object Hibernateobject is a simple POJO with some recommended good practices as below: There must be a default constructor for the class. There must be accessors and optional mutators for all the instance variables of the class. (Immutable objcets are exceptional cases). Recommended - POJO implements Serializable.
  • 6.
    Hibernate Object (contd..,)Recommended - POJO should ideally have an ID instance variable, usually of type Long. Recommended - The mutator for the id property should be private, not public.
  • 7.
    Hibernate Object (contd..,)You should decide on a business key for the object and implement the equals and hashCode methods for it. You should add any extra type specific constructors (which should leave the id field null) and business rule methods you like. You should not make the class final if you want to be able to use lazy loading for objects of the class.
  • 8.
    Hibernate Architecture terminologySessionFactory A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider. Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level. Session A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection. Factory for Transaction. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.
  • 9.
    Database Interaction Pattern1. Create a Configuration object, load the configuration parameters from the hibernate.properties and adjust them as required. 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. 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. 4. Inside a try block, get a Transaction object by calling beginTransaction() on the Session object.
  • 10.
    Hibernate - DatabaseInteraction Pattern (contd..,) 5. Interact with the database: 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. implicitly by calling property mutators on mapped objects that will lead to the database being updated. 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. implicitly by unreferencing mapped objects from other mapped objects which (in certain circumstances) can cause the unreferenced objects to be deleted from the database. 6. Call commit() on the Transaction object and close the try block, handling exceptions and closing the Session object in the usual way.
  • 11.
  • 12.
    Hibernate Object -Life Cycle 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. Persistent objects DO have an association with the database. 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.
  • 13.
    Built-in Mapping Types– Primitive Types
  • 14.
    Built-in Mapping Types– Date and Time Types
  • 15.