+ All Categories
Home > Documents > Become the nhibernate Guru

Become the nhibernate Guru

Date post: 22-Feb-2016
Category:
Upload: royal
View: 58 times
Download: 0 times
Share this document with a friend
Description:
Become the nhibernate Guru. Torkel Ödegaard. www.codinginstinct.com. Nhibernate?. ORM (Object-Relational Mapper). Transparent persistance. Persistence ignorance. The problem?. Identity map (L1 Cache). Unit Of Work (Session Flush). Cascading. Attached & Detached. - PowerPoint PPT Presentation
50
Become the nhibernate Guru
Transcript
Page 1: Become the nhibernate Guru

Become the nhibernate Guru

Page 2: Become the nhibernate Guru

Torkel Ödegaard

www.codinginstinct.com

Page 3: Become the nhibernate Guru

Nhibernate?ORM (Object-Relational Mapper)

Transparent persistance

Persistence ignorance

Page 4: Become the nhibernate Guru

The problem?Identity map (L1 Cache) Unit Of Work (Session Flush) Cascading

Attached & Detached Lazy loading & Proxy Objects

ID generation & unsaved-value Concurrency

Page 5: Become the nhibernate Guru
Page 6: Become the nhibernate Guru

Order

OrderLine

Customer

CorporateCustomer

PersonalCustomer

*

*

Page 7: Become the nhibernate Guru

IStarShip

Page 8: Become the nhibernate Guru

Those WERE the droids I was looking for...

Page 9: Become the nhibernate Guru
Page 10: Become the nhibernate Guru

MAPPing

Page 11: Become the nhibernate Guru

Mapping

Page 12: Become the nhibernate Guru

example

Page 13: Become the nhibernate Guru

unit of work

Insert entity #1

Insert entity #2

Update entity #3Delete collection entity

#5 Update collection entity

#6

Delete entity #8

session.Flush()

Page 14: Become the nhibernate Guru

Transaction

Unit Of Work

Page 15: Become the nhibernate Guru

Session management

Page 16: Become the nhibernate Guru

Session management

Page 17: Become the nhibernate Guru

Session management

Page 18: Become the nhibernate Guru

Session management

Page 19: Become the nhibernate Guru

identity map

True

Page 20: Become the nhibernate Guru

identity map

True

Id ModelName PilotId1 Bomber 22 Interceptor 2

Page 21: Become the nhibernate Guru

identity map - Problems

Same instance

Will return instance from identity map

Page 22: Become the nhibernate Guru

identity map - Problems

Two instances with same id

NHibernate.NonUniqueObjectException A different object with the same identifier value was already associated with the session

Page 23: Become the nhibernate Guru

identity map - Problems

NHibernate.HibernateExceptionIdentifier of an instance of TieFighter was altered …

Page 24: Become the nhibernate Guru

Cascading

Page 25: Become the nhibernate Guru

Cascading

Page 26: Become the nhibernate Guru

cascading• None• Save-update• Delete• Delete-orphan• All• All-delete-orphan

Page 27: Become the nhibernate Guru

Cascade & Save-update

Transient (unsaved) instance

Transient (unsaved) instance

INSERT INTO Pilot ....INSERT INTO TieFighter ...

Page 28: Become the nhibernate Guru

Cascade & delete

DELETE TieFighter ...DELETE Pilot ...

Page 29: Become the nhibernate Guru

Transient (unsaved) instance

Transient (unsaved) instance

NHibernate.TransientObjectException Object references an unsaved transient instance -save the transient instance before flushing

Cascade & None

Page 30: Become the nhibernate Guru

Cascade & none

INSERT TieFighter ...

Page 31: Become the nhibernate Guru

Cascade & none

Page 32: Become the nhibernate Guru

Cascade & none

UPDATE Pilot ...

Page 33: Become the nhibernate Guru

Why???? I did set cascade = none

Page 34: Become the nhibernate Guru

Cascade & none

UPDATE Pilot ...

Page 35: Become the nhibernate Guru

Attached / Detached

Page 36: Become the nhibernate Guru

Attached

NHibernate.HibernateExceptionIllegal attempt to associate a collection with twoopen sessions

Page 37: Become the nhibernate Guru

Detached

*

Page 38: Become the nhibernate Guru

Attached / Detached

Page 39: Become the nhibernate Guru

Detached

Updating a detached instance

INSERT INTO TieFighter ...UPDATE StarDestroyer ...UPDATE TieFighter #1 ...UPDATE TieFighter #2 ...UPDATE TieFighter #3 ...UPDATE TieFighter #4 ...UPDATE TieFighter #5 ...

Page 40: Become the nhibernate Guru

The ideal

Page 41: Become the nhibernate Guru

ReassociateReassociate (attaches)

instance to session

INSERT INTO TieFighter ...

Page 42: Become the nhibernate Guru

Merge Modified detached Instance

Attached instance with modifications

SELECT ... FROM StarDestroyer WHERE ...SELECT ... FROM TieFigher WHERE ...INSERT INTO TieFighter ...

Page 43: Become the nhibernate Guru

Lazy Loading

Page 44: Become the nhibernate Guru

PRoxy

Page 45: Become the nhibernate Guru

Proxy

Page 46: Become the nhibernate Guru

PRoxy

Page 47: Become the nhibernate Guru

unsaved-value

Page 48: Become the nhibernate Guru

Concurrency UPDATE PilotSET Version = 2 Name = ’Boba Fett’

Ranking = 1WHERE Id = ’62E96390-DDB4-493A-A216-62B45C86312’

AND Version = 1

NHibernate.StaleObjectException

Page 49: Become the nhibernate Guru
Page 50: Become the nhibernate Guru

• http://nhforge.org/• http://ayende.com/blog• http://www.codinginstinct.com• http://sharparchitecture.net/ • http://nhprof.com

resources

[email protected]


Recommended