开发者

How to query many to many relationship in NHibernate?

I am very new to NHibernate, and I would like to start development using this great technology.

Below is my table relation ship:

Table schemas

How to query many to many relationship in NHibernate?

http://dl.dropbox.com/u/33698629/Entities.JPG

tblAirport
  AirportID
  AirportShortName
  AirportFullName
  AirportTel

tblGroup
  GroupID
  GroupName

tblAirportGroup
  AirportID
  GroupID

I have defined the hbm.xml schemas as below:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Airport, TelDir.Core" table="tblAirport" lazy="false">
    <id name="ID" column="AirportID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="AirportShortName" column="AirportShortName" />
    <property name="AirportFullName" column="AirportFullName" />

    <set name="Groups" cascade="none" table="tblAirportGroup" lazy="false" access="readonly">
      <key column="AirportId"/>
      <many-to-many class="TelDir.Core.Doma开发者_运维百科in.Group, TelDir.Core" column="GroupId"/>
    </set>


  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Group, TelDir.Core" table="tblGroup" lazy="false">
    <id name="ID" column="GroupID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="GroupName" column="GroupName" />   

    <bag name="Airports" cascade="none" table="tblAirportGroup" lazy="false" >
      <key column="GroupID"/>
      <many-to-many class="TelDir.Core.Domain.Airport, TelDir.Core" column="AirportId"/>
    </bag>
  </class>
</hibernate-mapping>

Does my mapping is correct? How can I do to retrieve list of Group(GroupName) that belong to a Airport?

I have tried using this syntax

ISession session = NHibernateSessionManager.Instance.GetSession();
ISet<Group> rValue = session.CreateCriteria(typeof(Airport))
.CreateCriteria("Groups")
.Add(Expression.Eq("ID", ap.ID))
.List() as ISet<Group>;

but it give me no result.

Could you please guide me on that?

Best regards,


This is how you can get Groups that contain Airport with id 33 for example:

var groups = session.CreateCriteria<Group>()
                 .CreateCriteria("Airports")
                 .Add(Restrictions.Eq("ID", 33))
                 .List<Group>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜