[go: up one dir, main page]

0% found this document useful (0 votes)
4 views43 pages

NHibernet Fundamentals

The document provides guidance on using NHibernate for data persistence, including creating mapping files, inserting and updating data, and handling common issues like DateTime storage. It discusses performance optimization techniques such as using GUIDs and batch sizes, as well as strategies for managing relationships and lazy loading. Additionally, it covers querying with HQL and LINQ, emphasizing the importance of understanding when to use Get versus Load methods.

Uploaded by

Jignesh Sodagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views43 pages

NHibernet Fundamentals

The document provides guidance on using NHibernate for data persistence, including creating mapping files, inserting and updating data, and handling common issues like DateTime storage. It discusses performance optimization techniques such as using GUIDs and batch sizes, as well as strategies for managing relationships and lazy loading. Additionally, it covers querying with HQL and LINQ, emphasizing the importance of understanding when to use Get versus Load methods.

Uploaded by

Jignesh Sodagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

http://nhibernate.info/doc/nhibernate-reference/persistent-classes.

html
Add customer.hbm.xml

There tools available to add this but to understand w ehave added it


If assembly and namespace is not provide dthen we wneed to provide fully qualified name of
class)
In above we have used create criteria to fetch list of customers

We can also use linq for this using nhibernet.linq

Add log property to get the query that generated behind the scenes
Add nihibdernet profiler from nuget manager

Then initialize

Insert data
Update the data
Nhibernate fundas and basic mapping an
persistence
Check for code and convention based too
To Save enum as string value
Then do below

Common problems and solutions

DateTime.Kind is local time


To sav edate as local date time

If we store as utc then


Mapping and treading same table as different component
Usually when we use guid it gives performance effect and when you have more data – it doesn’t
have any increasing /decreasing index. To solve this problem using guild.comb option is best as it
is in always increasing order

Loquacious is which did above via configuration code


Now using xml

We can also mix and match

If we want we can even rename above config file and pass it to the

Cfg.Configure(“FileName”);

Also provide connection string intwo ways – direct string or name of connection placed in
app.config
Show_sql is similar to ShoqSqlInConsole to see generated sql

For performance generate statics is usefull

BatxhSize property is to control how many inserts you perfrom in batch

For this underlying database provider should support query batching


But if you use batchsize 10 in above case – and if you have generator type = native then it is going
to have 10 round trips to database as It needs to know what id has been generated

So in this case we should change it to guid.comb


Caching

Can be through xml file of class


Relationships
Backticks are for converting them to provider specific syntax in sql [Order] as it is keyword we
have put backticks (for added collections we use set)
But rather do ordering in c# cde
Above is when order is updated or deleted - in this case customer will not be affected
Lazy=false disabled lazy loading

Avoiding n+1 select means foreach order collections I need one specific line item

Fetch=join strategy gets the results intantaly of associated entity with outer join. By default
hibernet does 2 round trips one for main record then second when we access associated entity

Above is also doing same thing as mentioned – (eager load ) comes with associated entity

Below is selct n+1 select example


35 hits to db

1 for customer and 34 for rest of the orders (n+1)

Solution to above using fetch join


https://dzone.com/articles/playing-nhibernate-inverse-and

Many to one doesn’t have inverse property ad it always is set from that from the owned tem,from
the child item.
GET VS LOAD

Get fires call to database immediately while load does not need to access database immediately
and returns a proxy object when you issue that Load call -it can optimize database round trips.

If later in when you access that proxy, the object doesn’t happen to be in database – it can throw
an object not found the exception at that point.

And if you have akready called load on particular object then next time it will return actually
object instead of proxy as object is already loaded.
To perform query use hibernet query language
Aboce customer is class name and firstname is property name (not database table and field name)
Hql is very old and returns array (if we don’t cast as in above ) it will return array object

When you use queryhover – u need to use method chaining mix (conventional linq syntax wont
work) , and you can not mix xlassic cirteria and linq

You might also like