Download presentation
Presentation is loading. Please wait.
Published byMargery Long Modified over 8 years ago
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
4
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.
5
Why Hibernate? Minimizes Code Does not require a container Model is not tied to persistence implementation, Retains natural object model (transparent)
6
Object/Relational Mapping JavaObject int id; String name; String getName() int getId() void setName(String) void setId(int) SQL Table id [int] primary key, name [varchar(50)] Magic Happens Here (O/R Mapper – i.e. Hibernate)
7
Course Object Model
8
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
10
Persistence /Concurrency Pattern Introduced Martin Fowler 《企业应用架构模式》
11
Persistence What is Persistence Why Persistence is Importance History of persistence Plan text RDB(OODB?) XML
12
Mapping to a Relational Database Metadata Mapping Identity Field Lazy Load Query Object
13
Metadata Mapping Hold details of object-relational mapping in metadata. Hibernate as sample
14
Identity Field Save a database id field in an object to maintain identity between an in-memory object and a database row.
15
Identity Field Choosing your Key meaningful key meaningless key simple key compound key table-unique key database-unique key
16
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
17
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.
18
Lazy Load An object that doesn't contain all of the data you need, but knows how to get it.
19
Query Object An object that represents a database query SQL can be an involved language, and many developers are not particularly familiar with it..
20
Query Object( cont. )
21
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
22
Execution Contexts Connection Session A session is a long running interaction between a client and server Transaction Process Thread
23
Transactions The primary tool for handling concurrency in enterprise applications is the transaction. ATM machine example
24
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.
25
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,
26
Business and System Transactions System transactions transactions supported by RDBMS systems and transaction monitors Business transaction Transaction that logically defined by a business requirement
27
Concurrency Patterns Unit of work Optimistic Offline Lock Pessimistic Offline Lock
28
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.
29
How it Works Unit of Work is an object that keeps track of these changed things, such as inserted, updated or deleted.
30
Unit of Work
31
Optimistic Offline Lock Prevent conflicts between concurrent business transactions, by detecting a conflict and rolling back the transaction.
32
Optimistic Offline Lock
33
How it Works
34
Pessimistic Offline Lock Prevent conflicts between concurrent business transactions by allowing only one business transaction to access data at once
35
Pessimistic Offline Lock
36
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.