How to get same Columns from different tables - nhibernate

I have 3 tables
task (Id,text,Contact_Id)
users(Id,name)
company(id,name)
and 2 junction tables
task_users (task_id,user_id)
task_companies (task_id,company_id)
note: contact_id may be refer to users table or company
How can I get task id, task text, contact name in one Criteria

This's example with QueryOver, but it almost like ICriteria.
Contact contact = null;
mappingSession.QueryOver<Task>()
.JoinAlias(() => task.Contact, () => contact)
.SelectList(list => list
.SelectGroup(task => task.Id)
.Select(task => task.Text)
.Select(() => contact.Name))
.TransformUsing(Transformers.DistinctRootEntity)
.List();
Other is a mapping. You can make base entity "Contact" and extend from it User and Company.

Related

NHibernate.QueryException: not an association: Id

I wrote a joint query using NHiberNate, but I am getting a NHibernate.QueryException:not an association: Id
This is what it looks like with NHibernate library
TicketRequest ticketAlias = null;
Show showAlias = null;
IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
.JoinAlias(() => ticketAlias.ShowId, () => showAlias.Id)
.Where(() => showAlias.ShowDate >=DateTime.Now)
.List();
return results;
I just want a simple joint statement, and this is what it would have been in SQL
select * from TicketRequest as a join Show as b
on a.Show_id = b.Id
where ShowDate >=GETDATE()
Can someone help and let me know why I am getting a "not an association:Id" error. I have id in the "Show" table, it is a primary key.
Please advise. All helps are greatly appreciated.
You need to specify a many-to-one relation in joins. In your case that is the Show property.
IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
.JoinAlias(() => ticketAlias.Show, () => showAlias)
.Where(() => showAlias.ShowDate >= DateTime.Now)
.List();
PS: You shouldn't map both a many-to-one relation (Show) and an foreign key property (ShowID). Usually you only work with object relations when using an ORM. Only map the plain ID if you really need it for something, but even then only map it as read-only.
You don't have to specify the foreign keys / primary keys when querying with NHibernate. It's an ORM. You write object oriented queries. The keys and relations are specified in the mapping file.
A Join in an NHibernate query is simply specified by the property name with which you navigate to the other property.
That's what the error message means. Id is not an association.

NHibernate mapping in n:n relatinship

Suppose say I have Order, Items, OrderItems tables with Order and Items having n:n relationship and OrderItems being associative table. I have seen below two approaches for defining them.
1.Create Order and Items entities with "HasMany" relationship with OrderItems.
2.Create Order, Items and OrderItems entities with Order and Items having "ManytoMany" relationship and "OrderItems" containing Order and Item properties.
I have approach 1 which works fine but would like to know what approach 2 does.
If the relationship between Items and Orders is simple (merely that the relationship exists), then you would do a ManyToMany mapping between Items.Orders and Orders.Items. This would result in NHibernate generating and managing a simple cross reference table containing the two foreign keys.
On the other hand, if there is additional information that you need to record along with the two foreign keys, you must use a distinct entity or value object to capture that information, using HasMany on both sides.
Classes:
Order
Id
Name
OrderItems
Item
Id
Name
OrderItems
OrderItem
Id
Order
Item
Quantity
Mappings:
Order:
Id(c => c.Id);
Map(c => c.Name);
HasMany(c => c.OrderItems).KeyColumn("OrderId");
Item:
Id(c = c.Id);
Map(c => c.Name);
HasMany(c => c.OrderItems).KeyColumn("ItemId");
OrderItem:
Id(c => c.Id);
Map(c => c.Quantity);
References(c => c.Order);
References(c => c.Item);

Fluent nHibernate Many to Many Mapping

I have two tables called Users and Roles , and a bridge table to form many to many relation between users and roles.
My Question is that how can i create mapping for many to many relation in fluent nHibernate.
table User :
UserID
UserName
Password
FullName
Table Roles:
RoleID
RoleName
Description
Table Bridge:
UserID
RoleID
I have mapping tblUser like this
class tblUsersMap : ClassMap<tblUsers>
{
public tblUsersMap()
{
Id(user => user.UserID).GeneratedBy.Identity();
Map(user => user.UserName).Not.Nullable();
Map(user => user.Password).Not.Nullable();
Map(user => user.FullName).Not.Nullable();
}
}
and same way for mapping for Role table , but how can i define many to many mapping there?
Thanks
see here

Using finders to query a referenced model in Rails 3?

I have a model called Person that has_one Address. Person has fields like first_name and last_name; Address has postal_code, state, and so on.
I know how to use finders to find all people with a certain name; that's Person.find_by_last_name("Smith"). But how do I find all people where:
their first_name is "Bob"; and
their last_name is "Smith"; and
who have addresses with a city of 'Foobar'; and
who have addresses with a postal_code of '12345'
When you're looking for exact values, you can pass a hash of the values you'd like to match to where. Each referenced model is its own nested hash, as in the example below:
Person.
where(:first_name => 'Bob').
where(:last_name => 'Smith').
where(:address => { :postal_code => '12345' }). # queries associated Address
where(:address => { :city => 'Foobar' }) # queries associated Address

Rails problems with Join search

I am developing a small user application in Rails 3 and is stuck on a search feature. I got a table called Profile which holds Firstname and Lastname. When searching this table I use:
Profile.find_all_by_firstname(params[:keyword])
The thing is that I want the user to be able to search a table that holds contacts (profile_id and friend_id only) but I want them to be able to seach by name here too. How is this done?
For example:
John searches for the name Ann which is a contact of his. Since the Contact table does not store names the search must include the Profile table in the query. How can I do this?
UPDATE
This join query fetches everyone not only the contacts, can anyone spot a problem with it?
Profile.find_all_by_firstname(params[:keyword],
:select => "firstname, lastname, user_id",
:joins => "left outer join contacts on profiles.id = contacts.profile_id")
Profile.find_all_by_firstname(params[:keyword],
:select => "firstname, lastname, user_id",
:joins => "left outer join contacts on profiles.id = contacts.profile_id")
this query fetches everyone because you serch only by the firstname If you whant select contacts that are friends of the particular user firstly you must have his id Then you should add this to your conditions
current_user = Profile.first
Profile.find(:all,
:conditions => ["profiles.firstname = ? AND contacts.friend_id = ?", params[:keyword], current_user.id],
:joins => :contacts)
or make join conditional
current_user = Profile.first
Profile.find_all_by_firstname(params[:keyword],
:select => "firstname, lastname, user_id",
:joins => "INNER JOIN contacts on (profiles.id = contacts.profile_id AND contacts.friend_id = #{current_user.id})")
but I'm not quite sure about syntax