How to write Queries for retreving, Checking Existence in NHIBERNATE USING(FLUENTNHIBERNATE) - nhibernate

How to write Queries for retreving, Checking Existence, and all we do in SQL
using NHIBERNATE (FLUENTNHIBERNATE)
Is FluentNhibernate is used in these Queries.
And
Which one is better Fluent Nhibernate Mapping or XML Mapping
Is there any relation or should i know LINQ for learning Nhibernate?

i can see several questions:
"How to write Queries for retreving, Checking Existence": see here for the different querylanguages in NH http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html
"Is FluentNhibernate is used in these Queries": no FNH is used to configure NHibernate: alternative for hbm.xml and hibernate.cfg
"Which one is better Fluent Nhibernate Mapping or XML Mapping": in most cases FluentNHibernate is much easier to write than xml and covers 95% of options possible in xml. For beginners i would recommend FluentMappings because a lot of sensible defaults make it easier to get something to work.
"Is there any relation or should i know LINQ for learning Nhibernate?": LINQ is one of 6 ways to query in NHibernate. It is an elegant way but neither required nor the "works in all circumstances".

Related

NHibernate Query Generator with Fluent mappings

I was trying to find a good way to build dynamic queries with NHibernate and that lead me to NHibernate Query Generator which I like but we currently use Fluent NHibernate mappings and I wasn't sure if there was a way to use NHibernate Query Generator with Fluent NHibernate mappings. Any one have experience with this or know a better way to accomplish dynamic queries?
First, NHibernate already has ways to build dynamic queries (Criteria / QueryOver are specially useful for this). I googled "NHibernate Query Generator" and found a project that was abandoned more than 4 years ago.
Second, the method you use to create your mappings has nothing to do with the query APIs.

how to write fluent nhibernate queries?

I'm new to fluent nhibernate and generally nhibernate.
I searched a lot on the net but I couldn't find a good help.
I'm using fluent nhibernate and I mapped it with SQLServer 2008.
If there is a good help or book or sth else please tell me!
First of all I want to know how I can write native sql queries in my program? for example
DELETE * From SiteCategory
And generally I want to know how to write queries in fluent nhibernate? such as update,delete and select.
A good resource would be this series of screencasts:
Vacation Of Fluent NHibernate:
http://iamnotmyself.com/2009/10/26/vacation-of-fluent-nhibernate-introduction/
[link updated]
It was modeled after the very popular Summer of NHibernate series

From C# to hbm.xml

I'm looking for a NHibernate Mapping generator which could generate a mapping file (hbm) and SQL Script from a C# class. I've found generators for the other way around, but not for this case.
Can you guys point me to nice generator?
Thanks a lot!
With Fluent NHiberate you can do without mapping files. In the instances where the automapping doesn't work, you can override.
This post discusses schema generation
Fluent Nhibernate Schema Generation
The newest, fastest and most extensible way to configure NHibernate is ConfORM.
You don't need to generate xml files (although you can do so if you want). It's an order of magnitude faster than Fluent NHibernate and, overall, less work.
For some examples, read Fabio Maulo's posts.

Help me choose between linq to sql and nhibernate based on the following

Struggling between choosing linq2sql and nhibernate.
Let me give you some insight in the application in point form:
this is a asp.net mvc application
it will have lots of tables, maybe 50-60 (sql server 2008)
i would want all the basic crud logic done for me (which I think nhiberate + repository pattern can give me)
i don't have too complicated mappings, my tables will look something like:
User(userID, username)
UserProfile(userID, ...)
Content(contentID, title, body, date)
Content_User(contentID, userID)
So in general, I will have a PK table, then lots of other tables that reference that PK (i.e. FK tables).
I will also have lots of mapping tables, that will contain PK, FK pairs.
Entity wise, I want User.cs, UserProfile.cs and then a way to load each object.
I am not looking for a User class that has a UserProfile property, and a Content Collection property (there will be maybe 10-20 tables that will related to the user, I just like to keep things linear if that makes sense).
The one thing that makes me learn towards nhibernate is: cross db potential, and the repository pattern that will give me basic db operations accross all my main tables almost instantly!
Since you seem to have a quite straight forward mapping from class to table in mind Linq to SQL should do the trick, without any difficulties. That would let you get started very quickly, without the initial work of mapping the domain manually to the database.
An alternative could be using NHibernate + Fluent NHibernate and its AutoMapping feature, but keep in mind that the Fluent NHibernate AutoMapping is still quite young.
I'm not quite sure I understand what you want your entities to look like, but with Linq to SQL you will get a big generated mess, which you then could extend by using partial classes. NHibernate lets you design you classes however you want and doesn't generate anything for you out of the box. You could kind of use POCO classes with Linq to SQL but that would take away all the benefits of using Linq to SQL rather than NHibernate.
Concerning the repository pattern, and the use of a generic repository, that can be done quite nicely with Linq to SQL as well, and not only with NHibernate. In my opinion that is one of the nice things about Linq to SQL.
If you probably will need support for other databases than SQL Server, NHibernate is the only choice. However, if it probably won't be an issue I would recommend not using that as the primary factor when choosing. The other factors will probably influence your project more.
Conclusion:
All in all, I would recomment Linq to SQL, in this case, since it would let you get started quickly and is sufficient for your needs. The precondition for that is that you don't have a problem with the thought of having generated, messy code in your domain, and that you are quite sure there will not be any need to support other databases in the future. Otherwise I would recommend NHibernate, since it is truly an awesome ORM.
linq2sql really wants you to work with 1 table per class mapping. So if you have a UserMaster and a UserDetail table, you are looking at two objects when using default linq object generation. You can get around that by mapping linq entities to business entities (see Rob Conery's storefront screencasts), but then you are back to writing object mapping code or using something like Automapper.
If you want to be able to split your classes across multiple tables, then I'd say go with NHibernate. If not, then linq has a lower learning curve.
The only way I'd ever use nHibernate in through Castle Project's ActiveRecord library. Otherwise, nHibernate becomes its own little infrastructure project. Check out some questions in the nHibernate tag to see what I'm talking about.
The only thing I might change about AR is to return results of SELECT operations as List instead of T[]. Of course, with the source code in C# I can do that if I want.
With ActiveRecord, the mapping information is saved in attributes you decorate your classes with. It's genius and I am a huge proponent of the pattern and this particular library.

nHibernate - Generate classes from a database?

I know that you can generate a database from classes and their mappings using nHibernate, but I can't find any good links to do this the other way around. We have a database that has already been designed, and we are looking at using nHibernate. It would be nice to use a tool to generate the mappings and classes from the database, and then we could tweak/manipulate them to suit our tastes.
NHibernate Mapping Generator
You can use nhibernate with an existing database. It is a matter of writing the mapping files.
I also recommend using Fluent Nhibernate...I started using this after this community recommended it..
Look at subsonic as well if you do not like maintenance of mappings.