My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Friday, April 13, 2012

Hibernate in a pocket


Points to remember
-----------------
Identity
Equality
SubTypes - inheritence using sub and super classes
Granularity
Associations
Object Graph navigation - minimize the numebr of requests to DB

Why ORM
------
Productivity
Maintainability
Performance
Vendor independence
Two-Phase Commits


Points to Ponder for ORM
-----------------------
1) Core Interfaces called by applications to perform basic CRUD
 Session, SessionFactory,Configuration,Transaction,Query and Criteria
2) Callback interfaces that allow the application to react to events occurring inside Hibernate
 Interceptor, Lifecycle, and Validatable.
3) Interfaces that allow extension of Hibernate’s powerful mapping functionality
 Dialect,PropertyAccessor,Cache and CacheProvider,UserType, CompositeUserType, and IdentifierGenerator
4) makes use of existing Java APIs, including JDBC), Java Transaction API (JTA, and Java Naming and Directory Interface (JNDI). 
5) Configuration in managed environments using Resource Manager and Transaction Manager
6) Configuration in non-managed environments using Conenction Pool and user-managed JDBC Connections.
7) write-behind 
 Hibernate executes SQL statements asynchronously. SQL statements are usually issued at the end of a transaction.
8) Addressing leakage of concerns
 Domain model implementation shouldn’t depend on other Java APIs. shouldn’t perform JNDI lookups or call the database via the JDBC AP
9) Transparent and automated persistence
 complete separation of concerns between the persistent classes of the domain model  and the persistence logic
10) Hibernate doesn’t require that persistent classes implement Serializable. However, when objects are stored in an HttpSession or passed by value using RMI, serialization is necessary.
11) Hibernate requires a constructor with no arguments for every persistent class
12) object/relational mapping metadata
 ORM tools require a metadata format for the application to specify the mapping between classes and tables, properties and columns, associations and foreign keys,
 Java types and SQL types
13) The Hibernate mapping  DTD should be declared  in every mapping file Mappings are declared inside a <hibernate-mapping> element
14) Hibernate property mapping defines a JavaBeans property name, a database column name, and the name of a Hibernate type.
    derived properties 
         <property name="averageBidAmount"  formula="( select AVG(b.AMOUNT) from BID b ? where b.ITEM_ID = ITEM_ID )" type="big_decimal"/>
    property access strategies         
15) Support for fine-grained object models "more classes than tables" for implementing type-safety and behavior
16) An object of entity type has its own database identity (primary key value).
    Entity classes are always mapped to the database using <class>, <subclass>,and <joined-subclass> mapping elements
    An object of value type has no database identity; it belongs to an entity
17) aggregation "a part of" relationship. Aggregation is a strong form of association    
18) Inheritance
     Table per concrete class Discard polymorphism and inheritance
     Table per class hierarchy Enable polymorphism by denormalizing using "discriminator" and "subclass"
     Table per subclass Represent "is a" (inheritance) relationships as "has a"(foreign key) relationship using "joined-subclass"
19) Association
 Simple = using getter methods from parent to child. Mapping file of parent will have <many-to-one>  to class child.
 Bidirctional = mapping file of child will have <Set> <one-to-many> to class parent with inverse="true" 
20) The cascade attribute is directional: It applies to only one end of the association
 cascade="none"
  the default, tells Hibernate to ignore the association.
 save-update: 
  cascades save and update actions from the parent to the child.  Tells Hibernate to navigate the association when the
  transaction is committed and when an object is passed to save() or update() and save newly instantiated transient instances and persist changes to
  detached instances.
 delete: 
  cascades the delete action from the parent to the child. Delete children that are shared with existing parents
  tells Hibernate to navigate the association and delete persistent instances when an object is passed to delete()
 All: 
  all actions are cascaded from the parent to the child. All is the union of save-update and delete
  calls to evict and lock
 all-delete-orphan: 
  Hibernate will delete any persistent entity instance that has been removed
  all actions are cascaded from the parent to the child, orphan children are deleted
  means the same as cascade="all" but, in addition, Hibernate deletes any persistent entity instance that has been removed
 delete-orphan :
   
21) Transient objects 
 instantiated using the new operator aren’t immediately persistent. 
 Their state is transient, which means they aren’t associated with any database table row, and so their state is lost as soon as they’re dereferenced
22) Persistent objects
 objects instantiated by the application and then made persistent by calling the save()  
23) Detached objects - indicating that their state is no longer guaranteed to be synchronized with database state;
 When a transaction completes, the persistent instances associated with the persistence manager still exist.
 instances lose their association with the persistence manager when you close() the Session
24) persistence by reachability 
 instance becomes persistent when the application creates an object reference to the instance from another instance that is already persistent 
25) Fetching strategies
 Immediate fetching - The associated object is fetched immediately, using a sequential database read (or cache lookup). 
 Lazy fetching - The associated object or collection is fetched "lazily," when it’s first accessed. 
   This results in a new request to the database (unless the associated object is cached). 
 Eager fetching - The associated object or collection is fetched together with the owning object, 
    using an SQL outer join, and no further database request is required. 
 Batch fetching - This approach may be used to improve the performance oflazy fetching by retrieving a 
   batch of objects or collections when a lazy association is accessed. 
   (Batch fetching may also be used to improve the performance of immediate fetching.) 
26) Isolation problems
 Lost update
 Dirty read - One transaction reads changes made by another transaction that hasn’t yet been committed
 Unrepeatable read - reads a row twice and reads different state each time
 Second lost updates problem
 Phantom read - caused by another transaction inserting new rows between the execution of the two queries
27) Isolation levels
 Read uncommitted - Permits dirty reads but not lost updates
 Read committed - Permits unrepeatable reads but not dirty reads. momentary shared read locks and exclusive write locks
 Repeatable read - Permits neither unrepeatable reads nor dirty reads. using shared read locks and exclusive write lock
 Serializable - Provides the strictest transaction isolation
28) pessimistic lock 
 Lock that is acquired when an item of data is read and that is held until transaction completion 
 LockMode.NONE -  Don’t go to the database unless the object isn’t in either cache.
 LockMode.READ - Bypass both levels of the cache, and perform a version check to 
   verify that the object in memory is the same version that currently exists in the database
 LockMode.UPDGRADE - 
 LockMode.UPDGRADE_NOWAIT  - 
 LockMode.WRITE  -
29) Optimistic Locking
 version or timestamp columns
30) Collections
 Set
 Map and List - needs an index column to the database table
 Sorted collection  -  Sorted in memory using a Java comparator. 
 Ordered collection  -  ordered at the database level using an SQL query with an order by clause

No comments :