SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Professional Open Source™




           Data Access with Hibernate
           Persistent objects and persistence contexts




© JBoss, Inc. 2003, 2004.                                                 07/17/04   1
Object state transitions and Session methods
                                                                                      Professional Open Source™




                                new
                                                 Transient
                                                                                garbage
                                       save()
                               saveOrUpdate()                 delete()
                       get()
                      load()
                      find()
                   iterate()
                        etc.                     Persistent

                                       evict()                update()
                                      close()*                saveOrUpdate()
                                      clear()*                lock()
                                                                                garbage


                                                 Detached

                                                              * affects all instances in a Session




© JBoss, Inc. 2003, 2004.                                                                                         2
Transient objects
                                                                             Professional Open Source™


  Transient instances
           –     instances of a persistent class instantiated with the new operator
           –     transient, they have no persistent state
           –     garbage collected if dereferenced by the application
           –     have no database identity


  Transient instances may be made persistent by
           – calling Session.save(object)
           – creating a reference from another instance that is already persistent




© JBoss, Inc. 2003, 2004.                                                                                3
Persistent objects
                                                                       Professional Open Source™


  Persistent instances
           – have database identity
           – Include any instance retrieved with a query, lookup by identifier or
             navigation
           – are managed, changes are automatically flushed to the database
           – are transactional, changes can be rolled back in the database only
           – Persistent instances are always associated with a persistence context.




  Persistent instances may be made transient by
           – calling Session.delete(object)
           – “orphan delete” (later)




© JBoss, Inc. 2003, 2004.                                                                          4
Removed State
                                                             Professional Open Source™


  An object is in the removed state if it has been scheduled for deletion
   at the end of a unit of work, but it’s still managed by the persistence
   context until the unit of work completes.

  A removed object shouldn’t be reused because it will be deleted from
   the database as soon as the unit of work completes.




© JBoss, Inc. 2003, 2004.                                                                5
Detached objects
                                                                         Professional Open Source™


  Detached instances
           – are instances with database identity that are not associated with any open
             Session
           – are no longer managed by Hibernate
           – represent database state, that is potentially stale


  Persistent instances become detached by
           – calling Session.evict(object)
           – clearing the Session
           – closing the Session


  Detached instances become persistent by
           – calling Session.lock(object, lockMode)
           – calling Session.update(object, lockMode)
           – creating a reference from another instance that is already persistent


© JBoss, Inc. 2003, 2004.                                                                            6
The persistence context
                                                                  Professional Open Source™


  Persistence Context :
    – Persistence context can be considered to be a cache of managed entity
      instances.
    – Session has an internal persistence context
    – All entities in persistent state and managed in a unit of work are cached
      in this context.

  Use of Persistence context :
           – Hibernate can do automatic dirty checking and transactional write-
             behind.
           – Hibernate can use the persistence context as a first-level cache.
           – Hibernate can guarantee a scope of Java object identity.
           – Hibernate can extend the persistence context to span a whole
             conversation.




© JBoss, Inc. 2003, 2004.                                                                     7
Automatic dirty checking
                                                                     Professional Open Source™


           – Persistent instances are managed in a persistence context—their
             state is synchronized with the database at the end of the unit of
             work. When a unit of work completes, state held in memory is
             propagated to the database by the execution of SQL INSERT,
             UPDATE, and DELETE statements (DML).

           – Hibernate may synchronize with the database before execution of
             a query. This ensures that queries are aware of changes made
             earlier during the unit of work.

           – Hibernate doesn’t update the database row of every single
             persistent object in memory at the end of the unit of work. ORM
             software must have a strategy for detecting which persistent
             objects have been modified by the application. We call this
             automatic dirty checking. An object with modifications that have
                 not yet been propagated to the database is considered dirty.

© JBoss, Inc. 2003, 2004.                                                                        8
Transparent transaction-level write-behind
                                                                 Professional Open Source™


           – With transparent transaction-level write-behind, Hibernate
             propagates state changes to the database as late as possible but
             hides this detail from the application.



           – By executing DML as late as possible (toward the end of the
             database transaction), Hibernate tries to keep lock-times in the
             database as short as possible.




© JBoss, Inc. 2003, 2004.                                                                    9
About update queries
                                                                   Professional Open Source™


           – Hibernate is able to detect exactly which properties have been
             modified so that it’s possible to include only the columns that need
             updating in the SQL UPDATE statement.

           – By default, Hibernate includes all columns of a mapped table in
             the SQL UPDATE statement (hence, Hibernate can generate this
             basic SQL at startup, not at runtime).



           – If you want to update only modified columns, you can enable
             dynamic SQL generation by setting dynamic-update="true" in a
             class mapping




© JBoss, Inc. 2003, 2004.                                                                      10
Persistence Context cache
                                                            Professional Open Source™


  A persistence context is a cache of persistent entity instances. This
   means it remembers all persistent entity instances you’ve handled in
   a particular unit of work.

  Automatic dirty checking is one of the benefits of this caching.

  Another benefit is repeatable read for entities and the performance
   advantage of a unit of work-scoped cache.




© JBoss, Inc. 2003, 2004.                                                               11
The scope of object identity
                                                                         Professional Open Source™


  It is extremely important to understand the differences between
           – Java object identity: a == b
           – Database identity: a.getId().equals(b.getId()



                             The conditions when both are equivalent
                              is called the scope of object identity!




       Hibernate implements session-scoped identity – the two notions of
         identity are equivalent for instances associated with a particular
                                       session.




© JBoss, Inc. 2003, 2004.                                                                            12
The Hibernate identity scope
                                                                         Professional Open Source™




         Session session1 = sf.openSession();
         Transaction tx1 = session.beginTransaction();

         Object a = session1.load(Category.class, new Long(1234) );
         Object b = session1.load(Category.class, new Long(1234) );

         if ( a == b )
             System.out.println("a and b are identicial and the same database
         identity.");

         tx1.commit();
         session1.close();

         Session session2 = sf.openSession();
         Transaction tx2 = session.beginTransaction();

         Object b2 = session2.load(Category.class, new Long(1234) );

         if ( a != b2 )
             System.out.println("a and b2 are not identical.");

         tx2.commit();
         session2.close();




© JBoss, Inc. 2003, 2004.                                                                            13
Outside of the identity scope
                                                                        Professional Open Source™




                             In an instance becomes detached,
                             it leaves the scope of object identity.



       So, if we use detached instances in our application, we should not
              use == to test for identity. What should we use instead?




© JBoss, Inc. 2003, 2004.                                                                           14
Identity and equality contracts
                                                                                    Professional Open Source™


  Do we have to implement equals()and hashCode()?
           – the default implementation uses Java object identity
           – no good for detached objects
           – especially not if we put them in collections:

                            Session session1 = sf.openSession();
                            Transaction tx1 = session.beginTransaction();
                            Object itemA = session1.load(Item.class, new Long(1234) );
                            tx1.commit();
                            session1.close();

                            Session session2 = sf.openSession();
                            Transaction tx2 = session.beginTransaction();
                            Object itemB = session2.load(Item.class, new Long(1234) );
                            tx2.commit();
                            session2.close();

                            Set allObjects = new HashSet();
                            allObjects.add(itemA);
                            allObjects.add(itemB);
                            System.out.println(allObjects.size()); // How many elements? 2




© JBoss, Inc. 2003, 2004.                                                                                       15
Implementing equals() and hashCode()
                                                                            Professional Open Source™


  Could we compare identifiers?
           – for entities with surrogate keys, it is uninitialized for transient instances
           – identity of the instance changes when it becomes persistent, contrary to
             the contract of java.util.Set (the hashcode changes)

  Could we compare all properties except for the surrogate key?
           – identity of the instance changes when we modify the object, contrary to
             the contract of java.util.Set (the hashcode changes)
           – could potentially cause initialization of a whole graph of associated
             objects, just to evaluate equals()
           – two instances with the same database identity might not be equal!
           – Can two instances with different database identity be equal?


                                We need a business key.



© JBoss, Inc. 2003, 2004.                                                                               16
Using business keys for equality
                                                                                 Professional Open Source™


  A business key is a property or a combination of properties that is
           – unique for each instance with the same database identity
           – unique, constant and not null only for the comparison time span


                    public class Item {

                            public boolean equals(Object other) {
                                if (this == other) return true;
                                if (!(other instanceof Item)) return false;
                                final Item item = (Item) other;

                               if (!getSummary().equals(item.getSummary())) return false;
                               if (!getCreated().equals(item.getCreated())) return false;

                                return true;
                            }
                            public int hashCode() {
                                int result;
                                result = getSummary().hashCode();
                                return 29 * result + getCreated().hashCode();
                            }
                    }




© JBoss, Inc. 2003, 2004.                                                                                    17
Extending a persistence context
                                                              Professional Open Source™


  A particular conversation reuses the same persistence context for all
   interactions.
  All request processing during a conversation is managed by the same
   persistence context. The persistence context isn’t closed after a
   request from the user has been processed. It’s disconnected from the
   database and held in this state during user think-time. When the user
   continues in the conversation, the persistence context is reconnected
   to the database, and the next request can be processed.
  At the end of the conversation, the persistence context is
   synchronized with the database and closed.

       This eliminates the detached state .
       Also eliminates the need for manual reattachment or
       merging of object state between contexts .
       Will see how to use extended persistent context later …….


© JBoss, Inc. 2003, 2004.                                                                 18
The Hibernate Session
                                                                          Professional Open Source™


  The Hibernate Session is the persistence manager interface for
           –     basic CRUD (create, read, update, delete) operations (Session)
           –     query execution (Session, Query, Criteria)
           –     control of transactions (Transaction)
           –     management of the transaction-level cache


  At the beginning of a unit-of-work, the application thread
           – looks up the SessionFactory
           – obtains a Session



           A SessionFactory is expensive to create, a Session is not!
        In fact, a Session only obtains a JDBC connection when needed.




© JBoss, Inc. 2003, 2004.                                                                             19
Making an object persistent
                                                                           Professional Open Source™




                            User user = new User();
                            user.getName().setFirstName("John");
                            user.getName().setLastName("Doe");

                            Session session = sessions.openSession();
                            Transaction tx = session.beginTransaction();

                            session.save(user);

                            tx.commit();
                            session.close();




                 Hibernate executes SQL only as neccessary, in this case,
                   when the Transaction is committed. Hibernate uses
                      a transaction-scope write-behind strategy.




© JBoss, Inc. 2003, 2004.                                                                              20
Updating a detached instance
                                                                           Professional Open Source™



                            user.setPassword("secret");

                            Session sessionTwo = sessions.openSession();
                            Transaction tx =
                            sessionTwo.beginTransaction();

                            sessionTwo.update(user);

                            user.setLoginName("jonny");

                            tx.commit();
                            sessionTwo.close();

  The call to update() attaches the detached instance with the new
   Session, it doesn't matter if it's modified before or after the
   update(). Hibernate always treats the object as dirty and schedules
   an SQL UPDATE., which will be executed during flush. One way to
   avoid this UDPATE statement is to configure the class mapping of
   Item with the select-before-update="true“ attribute. Hibernate then
   determines whether the object is dirty by executing a SELECT
   statement and comparing the object’s current state to the current
   database state.

© JBoss, Inc. 2003, 2004.                                                                              21
Locking a detached instance
                                                                           Professional Open Source™




                            Session sessionTwo = sessions.openSession();
                            Transaction tx =
                            sessionTwo.beginTransaction();

                            sessionTwo.lock(user, LockMode.NONE);

                            user.setPassword("secret");
                            user.setLoginName("jonny");

                            tx.commit();
                            sessionTwo.close();




     Changes made before the call to lock() are not synchronized with
     the database. In this example, we don't even perform a version check
                 (LockMode.NONE), only reattach the object.




© JBoss, Inc. 2003, 2004.                                                                              22
Retrieving objects
                                                                               Professional Open Source™




                            Session session = sessions.openSession();
                            Transaction tx = session.beginTransaction();

                            int userID = 1234;
                            User user = session.get(User.class, new
                            Long(userID));
                            // "user" might be null if it doesn't exist
                            User user = session.load(User.class, new
                            Long(userID));
                            // will create only proxy . Will throw an
                            Exception //if row is not found in db when the proxy
                            is initialized
                            tx.commit();
                            session.close();




          Objects looked up by their identifier value are associated with a
             Session and automatically dirty-checked inside a Session.



© JBoss, Inc. 2003, 2004.                                                                                  23
Making a persistent object transient
                                                                           Professional Open Source™




                            Session session = sessions.openSession();
                            Transaction tx = session.beginTransaction();

                            int userID = 1234;
                            User user = session.get(User.class, new
                            Long(userID));

                            session.delete(user);

                            tx.commit();
                            session.close();




  Deleted objects are transient after the Session is closed and will be
   garbage collected if they are no longer referenced by other objects .
  But for that transient object, id will be the same. Hibernate can also
   roll back the identifier of any entity that has been deleted, if you
   enable the hibernate.use_identifier_rollback configuration option


© JBoss, Inc. 2003, 2004.                                                                              24
Making a detached object transient
                                                                             Professional Open Source™




                              Session session = sessions.openSession();
                              Transaction tx = session.beginTransaction();

                              session.delete(user);

                              tx.commit();
                              session.close();




                             Detached objects can be directly reattached and
                                 scheduled for deletion in a single call.




© JBoss, Inc. 2003, 2004.                                                                                25
Merging the state of a detached object
                                                              Professional Open Source™




            If the reattachment through update() clashes with this already
            persistent instance, a NonUniqueObjectException
            is thrown.
                     solution is to merge the two items




© JBoss, Inc. 2003, 2004.                                                                 26
Code for Merging
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               27

Weitere ähnliche Inhalte

Ähnlich wie 04 dataaccess

11 transitive persistence and filters
11 transitive persistence and filters11 transitive persistence and filters
11 transitive persistence and filtersthirumuru2012
 
10 conversations new
10 conversations new10 conversations new
10 conversations newthirumuru2012
 
02 hibernateintroduction
02 hibernateintroduction02 hibernateintroduction
02 hibernateintroductionthirumuru2012
 
12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategiesthirumuru2012
 
12 hibernate int&cache
12  hibernate int&cache12  hibernate int&cache
12 hibernate int&cachethirumuru2012
 
Oracle essbase 11.1.1 vs 11.1.2
Oracle essbase 11.1.1 vs 11.1.2Oracle essbase 11.1.1 vs 11.1.2
Oracle essbase 11.1.1 vs 11.1.2Vikrant Singh
 
01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modelingthirumuru2012
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking TourJoshua Long
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1b_kathir
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate IntroductionRanjan Kumar
 
OSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worldsOSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worldsArun Gupta
 
Hidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-PersistenceHidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-PersistenceSven Ruppert
 
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010Arun Gupta
 
Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01MongoDB
 
All your data belong to us - The Active Objects Plugin
All your data belong to us - The Active Objects PluginAll your data belong to us - The Active Objects Plugin
All your data belong to us - The Active Objects PluginSamuel Le Berrigaud
 
06 association of value types
06 association of value types06 association of value types
06 association of value typesthirumuru2012
 

Ähnlich wie 04 dataaccess (20)

11 transitive persistence and filters
11 transitive persistence and filters11 transitive persistence and filters
11 transitive persistence and filters
 
10 conversations new
10 conversations new10 conversations new
10 conversations new
 
02 hibernateintroduction
02 hibernateintroduction02 hibernateintroduction
02 hibernateintroduction
 
12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategies
 
Hibernate
HibernateHibernate
Hibernate
 
12 hibernate int&cache
12  hibernate int&cache12  hibernate int&cache
12 hibernate int&cache
 
Oracle essbase 11.1.1 vs 11.1.2
Oracle essbase 11.1.1 vs 11.1.2Oracle essbase 11.1.1 vs 11.1.2
Oracle essbase 11.1.1 vs 11.1.2
 
01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modeling
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking Tour
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
55j7
55j755j7
55j7
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate Introduction
 
OSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worldsOSGi & Java EE in GlassFish - Best of both worlds
OSGi & Java EE in GlassFish - Best of both worlds
 
Hidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-PersistenceHidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-Persistence
 
09 transactions new
09 transactions new09 transactions new
09 transactions new
 
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
OSGi & Java EE in GlassFish @ Silicon Valley Code Camp 2010
 
Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01
 
All your data belong to us - The Active Objects Plugin
All your data belong to us - The Active Objects PluginAll your data belong to us - The Active Objects Plugin
All your data belong to us - The Active Objects Plugin
 
06 association of value types
06 association of value types06 association of value types
06 association of value types
 

Mehr von thirumuru2012

Mehr von thirumuru2012 (7)

15 jpa
15 jpa15 jpa
15 jpa
 
15 jpa introduction
15 jpa introduction15 jpa introduction
15 jpa introduction
 
14 hql
14 hql14 hql
14 hql
 
14 criteria api
14 criteria api14 criteria api
14 criteria api
 
07 association of entities
07 association of entities07 association of entities
07 association of entities
 
05 inheritance
05 inheritance05 inheritance
05 inheritance
 
15 jpaql
15 jpaql15 jpaql
15 jpaql
 

Kürzlich hochgeladen

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

04 dataaccess

  • 1. Professional Open Source™ Data Access with Hibernate Persistent objects and persistence contexts © JBoss, Inc. 2003, 2004. 07/17/04 1
  • 2. Object state transitions and Session methods Professional Open Source™ new Transient garbage save() saveOrUpdate() delete() get() load() find() iterate() etc. Persistent evict() update() close()* saveOrUpdate() clear()* lock() garbage Detached * affects all instances in a Session © JBoss, Inc. 2003, 2004. 2
  • 3. Transient objects Professional Open Source™  Transient instances – instances of a persistent class instantiated with the new operator – transient, they have no persistent state – garbage collected if dereferenced by the application – have no database identity  Transient instances may be made persistent by – calling Session.save(object) – creating a reference from another instance that is already persistent © JBoss, Inc. 2003, 2004. 3
  • 4. Persistent objects Professional Open Source™  Persistent instances – have database identity – Include any instance retrieved with a query, lookup by identifier or navigation – are managed, changes are automatically flushed to the database – are transactional, changes can be rolled back in the database only – Persistent instances are always associated with a persistence context.  Persistent instances may be made transient by – calling Session.delete(object) – “orphan delete” (later) © JBoss, Inc. 2003, 2004. 4
  • 5. Removed State Professional Open Source™  An object is in the removed state if it has been scheduled for deletion at the end of a unit of work, but it’s still managed by the persistence context until the unit of work completes.  A removed object shouldn’t be reused because it will be deleted from the database as soon as the unit of work completes. © JBoss, Inc. 2003, 2004. 5
  • 6. Detached objects Professional Open Source™  Detached instances – are instances with database identity that are not associated with any open Session – are no longer managed by Hibernate – represent database state, that is potentially stale  Persistent instances become detached by – calling Session.evict(object) – clearing the Session – closing the Session  Detached instances become persistent by – calling Session.lock(object, lockMode) – calling Session.update(object, lockMode) – creating a reference from another instance that is already persistent © JBoss, Inc. 2003, 2004. 6
  • 7. The persistence context Professional Open Source™  Persistence Context : – Persistence context can be considered to be a cache of managed entity instances. – Session has an internal persistence context – All entities in persistent state and managed in a unit of work are cached in this context.  Use of Persistence context : – Hibernate can do automatic dirty checking and transactional write- behind. – Hibernate can use the persistence context as a first-level cache. – Hibernate can guarantee a scope of Java object identity. – Hibernate can extend the persistence context to span a whole conversation. © JBoss, Inc. 2003, 2004. 7
  • 8. Automatic dirty checking Professional Open Source™ – Persistent instances are managed in a persistence context—their state is synchronized with the database at the end of the unit of work. When a unit of work completes, state held in memory is propagated to the database by the execution of SQL INSERT, UPDATE, and DELETE statements (DML). – Hibernate may synchronize with the database before execution of a query. This ensures that queries are aware of changes made earlier during the unit of work. – Hibernate doesn’t update the database row of every single persistent object in memory at the end of the unit of work. ORM software must have a strategy for detecting which persistent objects have been modified by the application. We call this automatic dirty checking. An object with modifications that have not yet been propagated to the database is considered dirty. © JBoss, Inc. 2003, 2004. 8
  • 9. Transparent transaction-level write-behind Professional Open Source™ – With transparent transaction-level write-behind, Hibernate propagates state changes to the database as late as possible but hides this detail from the application. – By executing DML as late as possible (toward the end of the database transaction), Hibernate tries to keep lock-times in the database as short as possible. © JBoss, Inc. 2003, 2004. 9
  • 10. About update queries Professional Open Source™ – Hibernate is able to detect exactly which properties have been modified so that it’s possible to include only the columns that need updating in the SQL UPDATE statement. – By default, Hibernate includes all columns of a mapped table in the SQL UPDATE statement (hence, Hibernate can generate this basic SQL at startup, not at runtime). – If you want to update only modified columns, you can enable dynamic SQL generation by setting dynamic-update="true" in a class mapping © JBoss, Inc. 2003, 2004. 10
  • 11. Persistence Context cache Professional Open Source™  A persistence context is a cache of persistent entity instances. This means it remembers all persistent entity instances you’ve handled in a particular unit of work.  Automatic dirty checking is one of the benefits of this caching.  Another benefit is repeatable read for entities and the performance advantage of a unit of work-scoped cache. © JBoss, Inc. 2003, 2004. 11
  • 12. The scope of object identity Professional Open Source™  It is extremely important to understand the differences between – Java object identity: a == b – Database identity: a.getId().equals(b.getId()  The conditions when both are equivalent  is called the scope of object identity!  Hibernate implements session-scoped identity – the two notions of identity are equivalent for instances associated with a particular session. © JBoss, Inc. 2003, 2004. 12
  • 13. The Hibernate identity scope Professional Open Source™ Session session1 = sf.openSession(); Transaction tx1 = session.beginTransaction(); Object a = session1.load(Category.class, new Long(1234) ); Object b = session1.load(Category.class, new Long(1234) ); if ( a == b ) System.out.println("a and b are identicial and the same database identity."); tx1.commit(); session1.close(); Session session2 = sf.openSession(); Transaction tx2 = session.beginTransaction(); Object b2 = session2.load(Category.class, new Long(1234) ); if ( a != b2 ) System.out.println("a and b2 are not identical."); tx2.commit(); session2.close(); © JBoss, Inc. 2003, 2004. 13
  • 14. Outside of the identity scope Professional Open Source™  In an instance becomes detached,  it leaves the scope of object identity.  So, if we use detached instances in our application, we should not use == to test for identity. What should we use instead? © JBoss, Inc. 2003, 2004. 14
  • 15. Identity and equality contracts Professional Open Source™  Do we have to implement equals()and hashCode()? – the default implementation uses Java object identity – no good for detached objects – especially not if we put them in collections: Session session1 = sf.openSession(); Transaction tx1 = session.beginTransaction(); Object itemA = session1.load(Item.class, new Long(1234) ); tx1.commit(); session1.close(); Session session2 = sf.openSession(); Transaction tx2 = session.beginTransaction(); Object itemB = session2.load(Item.class, new Long(1234) ); tx2.commit(); session2.close(); Set allObjects = new HashSet(); allObjects.add(itemA); allObjects.add(itemB); System.out.println(allObjects.size()); // How many elements? 2 © JBoss, Inc. 2003, 2004. 15
  • 16. Implementing equals() and hashCode() Professional Open Source™  Could we compare identifiers? – for entities with surrogate keys, it is uninitialized for transient instances – identity of the instance changes when it becomes persistent, contrary to the contract of java.util.Set (the hashcode changes)  Could we compare all properties except for the surrogate key? – identity of the instance changes when we modify the object, contrary to the contract of java.util.Set (the hashcode changes) – could potentially cause initialization of a whole graph of associated objects, just to evaluate equals() – two instances with the same database identity might not be equal! – Can two instances with different database identity be equal? We need a business key. © JBoss, Inc. 2003, 2004. 16
  • 17. Using business keys for equality Professional Open Source™  A business key is a property or a combination of properties that is – unique for each instance with the same database identity – unique, constant and not null only for the comparison time span public class Item { public boolean equals(Object other) { if (this == other) return true; if (!(other instanceof Item)) return false; final Item item = (Item) other; if (!getSummary().equals(item.getSummary())) return false; if (!getCreated().equals(item.getCreated())) return false; return true; } public int hashCode() { int result; result = getSummary().hashCode(); return 29 * result + getCreated().hashCode(); } } © JBoss, Inc. 2003, 2004. 17
  • 18. Extending a persistence context Professional Open Source™  A particular conversation reuses the same persistence context for all interactions.  All request processing during a conversation is managed by the same persistence context. The persistence context isn’t closed after a request from the user has been processed. It’s disconnected from the database and held in this state during user think-time. When the user continues in the conversation, the persistence context is reconnected to the database, and the next request can be processed.  At the end of the conversation, the persistence context is synchronized with the database and closed.  This eliminates the detached state .  Also eliminates the need for manual reattachment or  merging of object state between contexts .  Will see how to use extended persistent context later ……. © JBoss, Inc. 2003, 2004. 18
  • 19. The Hibernate Session Professional Open Source™  The Hibernate Session is the persistence manager interface for – basic CRUD (create, read, update, delete) operations (Session) – query execution (Session, Query, Criteria) – control of transactions (Transaction) – management of the transaction-level cache  At the beginning of a unit-of-work, the application thread – looks up the SessionFactory – obtains a Session  A SessionFactory is expensive to create, a Session is not!  In fact, a Session only obtains a JDBC connection when needed. © JBoss, Inc. 2003, 2004. 19
  • 20. Making an object persistent Professional Open Source™ User user = new User(); user.getName().setFirstName("John"); user.getName().setLastName("Doe"); Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); session.save(user); tx.commit(); session.close();  Hibernate executes SQL only as neccessary, in this case,  when the Transaction is committed. Hibernate uses  a transaction-scope write-behind strategy. © JBoss, Inc. 2003, 2004. 20
  • 21. Updating a detached instance Professional Open Source™ user.setPassword("secret"); Session sessionTwo = sessions.openSession(); Transaction tx = sessionTwo.beginTransaction(); sessionTwo.update(user); user.setLoginName("jonny"); tx.commit(); sessionTwo.close();  The call to update() attaches the detached instance with the new Session, it doesn't matter if it's modified before or after the update(). Hibernate always treats the object as dirty and schedules an SQL UPDATE., which will be executed during flush. One way to avoid this UDPATE statement is to configure the class mapping of Item with the select-before-update="true“ attribute. Hibernate then determines whether the object is dirty by executing a SELECT statement and comparing the object’s current state to the current database state. © JBoss, Inc. 2003, 2004. 21
  • 22. Locking a detached instance Professional Open Source™ Session sessionTwo = sessions.openSession(); Transaction tx = sessionTwo.beginTransaction(); sessionTwo.lock(user, LockMode.NONE); user.setPassword("secret"); user.setLoginName("jonny"); tx.commit(); sessionTwo.close();  Changes made before the call to lock() are not synchronized with the database. In this example, we don't even perform a version check (LockMode.NONE), only reattach the object. © JBoss, Inc. 2003, 2004. 22
  • 23. Retrieving objects Professional Open Source™ Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); int userID = 1234; User user = session.get(User.class, new Long(userID)); // "user" might be null if it doesn't exist User user = session.load(User.class, new Long(userID)); // will create only proxy . Will throw an Exception //if row is not found in db when the proxy is initialized tx.commit(); session.close();  Objects looked up by their identifier value are associated with a Session and automatically dirty-checked inside a Session. © JBoss, Inc. 2003, 2004. 23
  • 24. Making a persistent object transient Professional Open Source™ Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); int userID = 1234; User user = session.get(User.class, new Long(userID)); session.delete(user); tx.commit(); session.close();  Deleted objects are transient after the Session is closed and will be garbage collected if they are no longer referenced by other objects .  But for that transient object, id will be the same. Hibernate can also roll back the identifier of any entity that has been deleted, if you enable the hibernate.use_identifier_rollback configuration option © JBoss, Inc. 2003, 2004. 24
  • 25. Making a detached object transient Professional Open Source™ Session session = sessions.openSession(); Transaction tx = session.beginTransaction(); session.delete(user); tx.commit(); session.close();  Detached objects can be directly reattached and  scheduled for deletion in a single call. © JBoss, Inc. 2003, 2004. 25
  • 26. Merging the state of a detached object Professional Open Source™ If the reattachment through update() clashes with this already persistent instance, a NonUniqueObjectException is thrown. solution is to merge the two items © JBoss, Inc. 2003, 2004. 26
  • 27. Code for Merging Professional Open Source™ © JBoss, Inc. 2003, 2004. 27