These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many-to-many and order-by
PostPosted: Thu Oct 18, 2007 1:20 pm 
Newbie

Joined: Thu Oct 18, 2007 12:55 pm
Posts: 2
I have a many-to-many relationship between Person and Group. When I access the members of a groups, I would like to sort by Lastname, Firstname. I tried to add the property order-by="Lastname, Firstname" on the bag Persons, but i have the error "Unknown column 'persons0_.Lastname' in 'order clause". NHibernate maps the order-by property on the table PersonGroup instead of Person. Is there a way make it work ?

Here's my mapping files :

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Person" table="Person">
    <id name="PersonID" column="PersonD" type="Int32" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Firstname" type="String" length="30" />
    <property name="Lastname" type="String" length="30" />
    <bag name="Groups" cascade="none" table="PersonGroup">
      <key column="PersonID"/>
      <many-to-many class="Group" column="GroupID"/>
    </bag>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Group" >
    <id name="GroupID" column="GroupID" type="Int32" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="Description" type="String" length="30"/>
    <bag name="Persons" cascade="none" table="PersonGroup" order-by="Lastname, Firstname">
       <key column="GroupID"/>
      <many-to-many class="Person" column="PersonID"/>
    </bag>
  </class>
</hibernate-mapping>


The SQL generated :
Code:
SELECT persons0_.GroupID as GroupID_1_,
persons0_.PersonID as PersonID1_,
person1_.PersonID as PersonID4_0_,
person1_.Firstname as Firstname4_0_,
person1_.Lastname as Lastname4_0_,
person1_.Birthdate as Birthdate4_0_
FROM PersonGroup persons0_
left outer join Persons person1_ on persons0_.PersonID=person1_.PersonID
WHERE persons0_.GroupID=?p0
ORDER BY persons0_.Lastname, persons0_.Firstname; ?p0 = '1'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 11:36 am 
Newbie

Joined: Thu Oct 18, 2007 12:55 pm
Posts: 2
I used order-by="person1_.Lastname, person1_.Firstname" as a temporary patch, I guess I'll have to make some HQL for my many-to-many relationships to be able to order by my data within the Query.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.