473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fetching data from multiple tables using nhibernate

Hi,
I have 3 tables in mysql
**User**
---------------------
`id` (auto increment)(PK)
`user_name`
`password`
`first_name`
`last_name`
`created_at`
`modified_at`
`email`

User_Role
-----------------------
`user_id` (PK)
`role_id` (PK)

Role
----------------------
`id`(PK)
`name`


The Mapping file User.hbm.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  3.                    assembly="WorkOrder.Model"
  4.                    namespace="WorkOrder.Model">
  5.   <class name="User" table="user">
  6.     <id name="id">
  7.       <generator class="identity"/>
  8.     </id>
  9.     <property name="UserName"  column="user_name" type="String"/>
  10.     <property name="Password"  column="password" type="String"/>
  11.     <property name="FirstName"  column="first_name" type="String"/>
  12.     <property name="LastName"  column="last_name" type="String"/>
  13.     <property name="CreatedAt"  column="created_at" type="DateTime"/>
  14.     <property name="ModifiedAt"  column="modified_at" type="DateTime"/>
  15.     <property name="Email"  column="email" type="String"/>
  16.   </class>
  17. </hibernate-mapping>
  18.  
  19. The Mapping file User_Role.hbm.xml
  20. <?xml version="1.0" encoding="utf-8" ?>
  21. <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  22.                            assembly="WorkOrder.Model"
  23.                            namespace="WorkOrder.Model">
  24.   <class name="User_Role" table="user_role">
  25.     <composite-id>
  26.       <key-property  name="UserId"  column="id" type="Integer"></key-property>
  27.       <key-property  name="RoleId"  column="role_id" type="Integer"></key-property>
  28.     </composite-id>    
  29.     <many-to-one class="User" name="User_Id" column="User_Id"  cascade="all"> </many-to-one>
  30.     <many-to-one class="Role" name="Role_Id" column="Role_Id"  cascade="all"> </many-to-one>
  31.   </class>
  32. </hibernate-mapping>
  33.  
  34. The Mapping file Role.hbm.xml
  35. <?xml version="1.0" encoding="utf-8" ?>
  36. <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  37.                    assembly="WorkOrder.Model"
  38.                    namespace="WorkOrder.Model">
  39.  
  40.   <class name="Role" table="role">
  41.  
  42.     <id name="RoleId" column="id">
  43.       <generator class="identity"/>
  44.     </id>
  45.     <property name="Name" column="name"  length="64"/>
  46.  
  47.   </class>
  48. </hibernate-mapping>
I want to fetch the information from these tables with the query

Expand|Select|Wrap|Line Numbers
  1. SELECT u.*,r.name 
  2. FROM user u
  3. JOIN user_role ur ON UR.user_id=u.id
  4. JOIN role r on UR.role_id=r.id
  5.  
I tried with this code
Expand|Select|Wrap|Line Numbers
  1. ICriteria Criteria = session.CreateCriteria(typeof(User));
  2.                 Criteria.CreateAlias("User_Role", "User_Role");
  3.                 Criteria.CreateAlias("User_Role.Role", "UsrRole");
  4.                 Criteria.Add(Expression.Eq("id", Uid));
but its showing error
can anyone please help?
Dec 28 '10 #1
0 1854

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ryan Hubbard | last post by:
I'm inserting a record into MySQL 4.0 using Visual Basic ADO. When using the AddNew and Update method I am unable to retrieve the value of a Auto incrment field (Yes I know I can MoveLast but this...
8
by: DB2 Novice | last post by:
I am trying to use DB2 Control Centre (version 8.2) to load one flat file into multiple tables. However, I don't see the options in Control Centre that allows that. Anyone knows how to do this?...
11
by: svdh2 | last post by:
I have been looking at a problem the entire week in access, I have still not been able to find a solution. Hope that you could maybe tell where to look Concerns the link between Access and Word....
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
9
by: sliderip | last post by:
In Access 2003, is it possible to delete multiple tables using a QUERY? I want to delete multiple tables but i guess i cant use wildcards with DROP i..e DROP TABLE mytable1% or something like...
2
by: chopin | last post by:
I am using Microsoft Access, and VBA. I was wondering if it was possible to select multiple tables using DAO. For example, here is the code I am thinking should work, but doesn't: sSQL =...
6
by: CoolForU | last post by:
This is my table tblName Pid PName Country ------------------------------------------------------------------ 100 Shantosh India 102 Mukesh India 103 Rakesh India
3
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.