-->
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: "order by" on the property of an association
PostPosted: Fri Mar 10, 2006 1:09 pm 
Newbie

Joined: Thu Mar 09, 2006 2:25 pm
Posts: 1
Hello,

after trying myself for quite a while and searching for answers in here, i'm slowly getting desparate. I think my problem should be easy to solve:

I have 2 Entities, Person and Company, where Person has a non-lazy, null-able many-to-one association to Company. Now I want to list all Persons ordered by the name of their associated Company. The hard part is that some Persons have no associated Company and I have no clue how to get the correctly ordered results.

I already tried different queries:

Code:
SELECT p FROM PersonImpl AS p ORDER BY p.hibCompany.hibCompanyName
--> this lists only Persons where p.hibCompany is not null

Code:
SELECT p FROM PersonImpl AS p LEFT JOIN p.hibCompany ORDER BY p.hibCompany.hibCompanyName
--> this lists all Persons but not ordered correctly

Help would be very, very appreciated... ;-)


This is a code snippet of my 2 Entities:

Code:
/**
*
* @hibernate.joined-subclass
*   table = "ACC_PERSON"
* @hibernate.joined-subclass-key
*   column = "PK"
*/
public class PersonImpl extends AbstractAccount implements Person {

  /**
   * @hibernate.many-to-one
   *   column = "FK_COMPANY"
   *   not-null = "false"
   *   not-found = "ignore"
   *   lazy = "false"
   *   access = "field"
   */
   private CompanyImpl hibCompany = null;
}


/**
* @hibernate.joined-subclass
*   table = "ACC_COMPANY"
* @hibernate.joined-subclass-key
*   column = "PK"
*/
public class CompanyImpl extends AbstractAccount implements Company {

/**
* @hibernate.property
*   column = "COMPANY_NAME"
*   not-null = "true"
*   type = "string"
*   access = "field"
*/
private String hibCompanyName = "";

/**
* @hibernate.set
*   inverse = "true"
*   cascade = "none"
*   access = "field"
* @hibernate.key
*   column = "FK_COMPANY"
* @hibernate.one-to-many
*   class = "com.goelz.marketingsuite.account.person.PersonImpl"
    */
private Set hibEmployees = new HashSet();
}




Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents: see above

Name and version of the database you are using: MySQL 4.1


Top
 Profile  
 
 Post subject: I'd try a variation of your second query
PostPosted: Fri Mar 10, 2006 1:56 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I would slightly modify your second query, like this:

Code:
SELECT
    p, c.hibCompanyName
FROM
    PersonImpl AS p
LEFT JOIN
    p.hibCompany as c
ORDER BY
    c.hibCompanyName

If this works, you'll need to discard the second element of each returned array. If this does not work, could you turn on SQL logging and post the SQL generated from the query?

I hope this helps.


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.