1 / 36

Hibernate Persistence

Hibernate is a popular open-source ORM tool that allows transparent persistence of POJOs to a relational database. It minimizes code, doesn't require a container, and retains a natural object model.

jerid
Télécharger la présentation

Hibernate Persistence

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hibernate Persistence

  2. What is Persistence • Persist data to database or other storage. • In OO world, persistence means persist object to external storage. • Relational database • SQL • Using SQL in Java • Persistence object-oriented applications: ORM

  3. What is Hibernate? • Popular Open Source (LGPL) Object/Relational Mapping (ORM) tool • Transparent persistence for POJOs (Plain Old Java Objects) • Core of JBoss CMP 2.0 impl.

  4. Why Hibernate? • Minimizes Code • Does not require a container • Model is not tied to persistence implementation, Retains natural object model (transparent)

  5. JavaObject SQL Table int id; String name; String getName() int getId() void setName(String) void setId(int) id [int] primary key, name [varchar(50)] Magic Happens Here (O/R Mapper – i.e. Hibernate) Object/Relational Mapping

  6. Course Object Model

  7. Persistence Tasks • Class mapping • Class to Table mapping • Object ID to Primary Key • Attribute Mapping • Primary Attribute to Column • User defined data type to embedded type • Inheritance Mapping • Relationship Mapping • One to Many • Many to One

  8. Persistence /Concurrency Pattern Introduced Martin Fowler《企业应用架构模式》

  9. Persistence • What is Persistence • Why Persistence is Importance • History of persistence • Plan text • RDB(OODB?) • XML

  10. Mapping to a Relational Database • Metadata Mapping • Identity Field • Lazy Load • Query Object

  11. Metadata Mapping • Hold details of object-relational mapping in metadata. • Hibernate as sample

  12. Identity Field • Save a database id field in an object to maintain identity between an in-memory object and a database row.

  13. Identity Field • Choosing your Key • meaningful key • meaningless key • simple key • compound key • table-unique key • database-unique key

  14. How to get a new Key • Database counter • it's non-standard and not available in all databases. • Key table • Separate transaction is needed • GUID • Large keys may also lead to performance problems, particularly with indexes

  15. Identity Map • Ensure each object only gets loaded once by keeping every loaded object in a map. Lookup objects using the map when referring to them • A Identity Map keeps a record of all the objects that have been read from the database in a single business transaction. Whenever you want an object, you check the Identity Map first to see if you already have it.

  16. Lazy Load • An object that doesn't contain all of the data you need, but knows how to get it.

  17. Query Object • An object that represents a database query • SQL can be an involved language, and many developers are not particularly familiar with it..

  18. Query Object( cont. )

  19. Concurrency • When Concurrency problem raised? • Why Concurrency is difficult to deal with • It is difficult to enumerate the possible scenarios that can get you into trouble • it is hard to test for

  20. Execution Contexts • Connection • Session • A session is a long running interaction between a client and server • Transaction • Process • Thread

  21. Transactions • The primary tool for handling concurrency in enterprise applications is the transaction. • ATM machine example

  22. Transaction Resources • Most enterprise applications run into transactions in terms of databases. But there are plenty of other things than can be controlled using transactions, such as • message queues, • printers, • As a result technical discussions of transactions use the term 'transactional resource' to mean anything that is transactional: that is uses transactions to control concurrency.

  23. Transaction types • long transaction. • making a transaction span multiple requests is generally known as a long transaction . • Not recommended • request transaction • start a transaction at the beginning of a request and complete it at the end. • late transaction • open a transaction as late as possible ,

  24. Business and System Transactions • System transactions • transactions supported by RDBMS systems and transaction monitors • Business transaction • Transaction that logically defined by a business requirement

  25. Concurrency Patterns • Unit of work • Optimistic Offline Lock • Pessimistic Offline Lock

  26. Unit of Work • Maintains a list of objects that are affected by a business transaction and coordinates the writing out of changes and resolution of concurrency problems.

  27. How it Works • Unit of Work is an object that keeps track of these changed things, such as inserted, updated or deleted.

  28. Unit of Work

  29. Optimistic Offline Lock • Prevent conflicts between concurrent business transactions, by detecting a conflict and rolling back the transaction.

  30. Optimistic Offline Lock

  31. How it Works

  32. Pessimistic Offline Lock • Prevent conflicts between concurrent business transactions by allowing only one business transaction to access data at once

  33. Pessimistic Offline Lock

  34. How it Works • have an exclusive write lock. That is, require only that a business transaction acquire a lock in order to edit session data. This avoids conflict by not allowing two business transactions to simultaneously make changes to the same record.

More Related