-->
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.  [ 5 posts ] 
Author Message
 Post subject: Sorting a Tier-2 Inheritance model by inherited attributes?
PostPosted: Fri Mar 19, 2004 5:05 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hi I have a class Contact, with fields lastName/firstName etc.. my mapping doc looks like:
Code:
<hibernate-mapping>
    <class name="Contact" table="CONTACTS">
      <id name="id" type="long" unsaved-value="null">
           <generator class="identity"/>
        </id>
        <property name="email"/>
        <property name="fax"/>
        <property name="firstName"/>
        <property name="gender"/>
        <property name="lastName"/>
        <property name="middleName"/>
        <property name="mobileNumber"/>
        <property name="note"/>
        <property name="phone"/>
        <property name="phoneExtension"/>
        <property name="preferredName"/>
        <property name="title"/>
        <many-to-one name="user" column="user_id"/>
        <joined-subclass name="PMSContact" table="PMSContact">
            <key column="CONTACT_ID"/>
            <many-to-one name="prospect" column="prospect_id" not-null="true"/>
        </joined-subclass>
    </class>
</hibernate-mapping>


I have another class that is one-to-many with the subclass PMSContact, I tried to have it sort on load by lastName, and it failed saying the column was not part of the object? Here's what I tried

Code:
<set name="contacts" cascade="all-delete-orphan" inverse="true" lazy="true">
    <key column="prospect_id"/>
    <one-to-many class="PMSContact" order-by"lastName asc"/>
</set>       


Here's the error thrown from my log file:
Code:
2004-03-19 13:57:45,765 [ERROR] util.JDBCExceptionReporter - could not initialize collection: [Prospect.contacts#1]
java.sql.SQLException: Column not found,  message from server: "Unknown column 'contacts0_.lastName' in 'order clause'"


Is this a limitation of ordering?
Thanks
David


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 20, 2004 11:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
this look normal, order-by works with column, not object attribute, have you tried the index with a map for example?
i know it works with column too, but maybe it generate the sql you need.

Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 3:32 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
hmm I don't understand what you mean index with a map... but I don't understand why order by in a set shouldnt work with attributes of the object, since thats what hibernate is modeling anyway. Is there some possible way to specify sorting on the parent table, like sort-by="super.lastName asc" or something of that nature? It just seems odd that this is one of the only things that doesn't support inheritance/polymorphism, when everything else does.

Even the Criteria queries do.. I ran a test case just to check:
Code:
      Criteria crit = session.createCriteria(PMSContact.class);
      crit.addOrder( Order.asc("lastName"));
      List PMSContacts = crit.list();


Works like a charm.. (PMSContact being the subclass, lastName being an attribute of the parent class). But I can't use that code when loading the class ;(, and I dreadfully don't want to have to create comparators for everything to do sorting...

Help!
-David


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 3:42 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
We just had a similar problem. We ended up making our class implement Comparable, and then in the <set> element we specified

Code:
sort="natural"


Or you could write your own implementation of Comparator and specify

Code:
sort="my.custom.ContactComparator"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 3:46 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Ya I wrote up a comparator to do it, but the app we are developing it would be nice to be able to sort based on any of the fields within it.. or at least the ones that have natural ordering to them (Strings ints, etc)... and I dread having to write a comparator anytime I want to modify that, very easy to just do a sort.. based on a column, sigh :P

I was picking through the source code to the queries and when its generating the sql based on the order-by column it inserts a $PlaceHolder$.lastName into the query then somewhere determines what to replace that with (the table obviously).. you'd think there would be some polymorphic logic in there or at least somewhere we could put some to determine that it needs to look to the correct place to find that column...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.