540 likes | 686 Views
Become the nhibernate Guru. Torkel Ödegaard. torkel.odegaard@gmail.com. www.codinginstinct.com. Nhibernate?. ORM (Object-Relational Mapper). Transparent persistance. Persistence ignorance. Become the Guru. Identity map (L1 Cache) Unit Of Work (Session Flush) Cascading
E N D
Torkel Ödegaard torkel.odegaard@gmail.com www.codinginstinct.com
Nhibernate? ORM (Object-Relational Mapper) Transparent persistance Persistence ignorance
Become the Guru • Identity map (L1 Cache) • Unit Of Work (Session Flush) • Cascading • Attached / Detached • Proxy / Lazy loading • ID generation / unsaved-value • Concurrency • Legacy databases • Performance
unit of work session.Flush()
Flush Mode • Auto • Commit • Never • Unspecified
Transaction Unit Of Work
identity map True
identity map True
identity map - Problems Same instance Will return instance from identity map
identity map - Problems NHibernate.NonUniqueObjectException A different object with the same identifier value was already associated with the session Two instances with same id
identity map - Problems NHibernate.HibernateException Identifier of an instance of TieFighter was altered …
cascading • None • Save-update • Delete • Delete-orphan • All • All-delete-orphan
Cascade & Save-update Transient (unsaved) instance Transient (unsaved) instance INSERT INTO Pilot .... INSERT INTO TieFighter ...
Cascade & delete DELETE TieFighter ... DELETE Pilot ...
Cascade & none Transient (unsaved) instance Transient (unsaved) instance NHibernate.TransientObjectException Object references an unsaved transient instance -save the transient instance before flushing
Cascade & none UPDATE Pilot ...
Attached NHibernate.HibernateException Illegal attempt to associate a collection with two open sessions
Detached INSERT INTO TieFighter ... UPDATE StarDestroyer ... UPDATE TieFigher #1 ... UPDATE TieFigher #2 ... UPDATE TieFigher #3 ... UPDATE TieFigher #4 ... UPDATE TieFigher #5 ... Updating a detached instance
Reassociate Reassociate (attaches) instance to session INSERT INTO TieFighter ...
Merge Modified detached Instance Attached instance with modifications SELECT ... FROM StarDestroyer WHERE ... SELECT ... FROM TieFigher WHERE ... INSERT INTO TieFighter ...
legacy • Composite primary keys • Relations on non-primary keys • Join • SQL Queries • DB Generated fields
performance • Batching • Eager loading (eliminate N+1) • Query Cache • L2 Cache