Re: Mapping Problem

35 views
Skip to first unread message

Ricardo Peres

unread,
Sep 17, 2012, 7:48:41 AM9/17/12
to nhu...@googlegroups.com
The where attribute must contain SQL, not HQL.

On Sunday, September 16, 2012 11:27:11 AM UTC+1, A_Doustkani wrote:
Hi. I'm new to NHibernate. I have an old database that I have to do some mappings on it. 
 The below database is an example of my problem (not the same).

Database:

Class Diagram:


I want to fill MaleActors with Actors that their Sex field equals to 'Male' and FemaleActors with Actors that their Sex field equals to 'Female'
I tried where attribute on my many-to-many relation but it doesn't work. 

Here are my Mapping files:

Movie.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MovieEg" namespace="MovieEg">
    <class name="Movie" table="Movies">
      <id name="Id">
        <generator class="identity"/>  
      </id>
      <property name="Name"/>
      <bag name="FActors" table="ActorRoles" >
        <key column="MovieId"/>
        <many-to-many class="Role" column="Id" where="Actors.Sex='Male'" />  
      </bag>
    </class>
</hibernate-mapping>

Actor.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MovieEg" namespace="MovieEg">
    <class name="Actor" table="Actors">
      <id name="Id">
        <generator class="identity"/>
      </id>
      <property name="Name"/>
      <property name="Sex"/>
    </class>
</hibernate-mapping>
 
ActorRole.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MovieEg" namespace="MovieEg">
<class name="Role" table="ActorRoles">
<id name="Id">
<generator class="identity"/>
</id>
<property name="RoleNameInMovie" column="Role"/>
  <many-to-one name="Actor" class="Actor" column="ActorId" />
</class>
</hibernate-mapping>

Ricardo Peres

unread,
Sep 18, 2012, 2:24:32 PM9/18/12
to nhu...@googlegroups.com
I suspect in this case it won't work, because you are not filtering on a column of the mapping table, but on a column on the other endpoint table.

RP

On Tuesday, September 18, 2012 4:35:29 AM UTC+1, A_Doustkani wrote:
Would you please give me an example?
Reply all
Reply to author
Forward
0 new messages