Hibernate Persistence What is Persistence n Persist data
Hibernate Persistence
What is Persistence n Persist data to database or other storage. q n In OO world, persistence means persist object to external storage. Relational database q q q SQL Using SQL in Java Persistence object-oriented applications: ORM
What is Hibernate? n n n Popular Open Source (LGPL) Object/Relational Mapping (ORM) tool Transparent persistence for POJOs (Plain Old Java Objects) Core of JBoss CMP 2. 0 impl.
Why Hibernate? • Minimizes Code • Does not require a container • Model is not tied to persistence implementation, Retains natural object model (transparent)
Object/Relational Mapping Java. Object int id; String name; String get. Name() int get. Id() void set. Name(String) void set. Id(int) SQL Table id [int] primary key, name [varchar(50)] Magic Happens Here (O/R Mapper – i. e. Hibernate)
Course Object Model
Persistence Tasks n Class mapping q q n Attribute Mapping q q n n Class to Table mapping Object ID to Primary Key Primary Attribute to Column User defined data type to embedded type Inheritance Mapping Relationship Mapping q q One to Many to One
Persistence /Concurrency Pattern Introduced Martin Fowler《企��用架构模式》
Persistence n n n What is Persistence Why Persistence is Importance History of persistence q q q Plan text RDB(OODB? ) XML
Mapping to a Relational Database n n Metadata Mapping Identity Field Lazy Load Query Object
Metadata Mapping n n Hold details of object-relational mapping in metadata. Hibernate as sample
Identity Field n Save a database id field in an object to maintain identity between an in-memory object and a database row.
Identity Field n Choosing your Key q q q meaningful key meaningless key simple key compound key table-unique key database-unique key
How to get a new Key n Database counter q n Key table q n it's non-standard and not available in all databases. Separate transaction is needed GUID q Large keys may also lead to performance problems, particularly with indexes
Identity Map n n 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.
Lazy Load n An object that doesn't contain all of the data you need, but knows how to get it.
Query Object n n An object that represents a database query SQL can be an involved language, and many developers are not particularly familiar with it. .
Query Object( cont. )
Concurrency n n When Concurrency problem raised? Why Concurrency is difficult to deal with q q It is difficult to enumerate the possible scenarios that can get you into trouble it is hard to test for
Execution Contexts n n Connection Session q A session is a long running interaction between a client and server n Transaction n Process Thread n
Transactions n n The primary tool for handling concurrency in enterprise applications is the transaction. ATM machine example
Transaction Resources n 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 q q n 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.
Transaction types n long transaction. q q n request transaction q n making a transaction span multiple requests is generally known as a long transaction. Not recommended start a transaction at the beginning of a request and complete it at the end. late transaction q open a transaction as late as possible ,
Business and System Transactions n System transactions q n transactions supported by RDBMS systems and transaction monitors Business transaction q Transaction that logically defined by a business requirement
Concurrency Patterns n n n Unit of work Optimistic Offline Lock Pessimistic Offline Lock
Unit of Work n Maintains a list of objects that are affected by a business transaction and coordinates the writing out of changes and resolution of concurrency problems.
How it Works n Unit of Work is an object that keeps track of these changed things, such as inserted, updated or deleted.
Unit of Work
Optimistic Offline Lock n Prevent conflicts between concurrent business transactions, by detecting a conflict and rolling back the transaction.
Optimistic Offline Lock
How it Works
Pessimistic Offline Lock n Prevent conflicts between concurrent business transactions by allowing only one business transaction to access data at once
Pessimistic Offline Lock
How it Works n 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.
- Slides: 36