-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hobernate order by 'many to one' property...
PostPosted: Thu Aug 30, 2012 9:33 am 
Newbie

Joined: Thu Aug 30, 2012 9:07 am
Posts: 3
Hey,

Here is the problem. Lets say we have a Product class which is mapped to 'product' table. And there is a 'supplier' property in it which is mapped as many to one to 'Person' class. supplier is a NULLABLE property...

@Entity
@Table(name="product")
public class Product {
...
...

@ManyToOne
@JoinColumn(name = "bp_supplier_id", nullable = true, insertable=true, updatable=true)
private Person supplier;
...
}

So, here is the problem. I am trying to select products and order the result by the supplier first name - something like this:

FROM Product product ORDER BY product.supplier.firstName

As a result we will get all products EXCEPT those where the supplier is NULL.
So to have also those products included in the result (where supplier is null) I've always used CASE WHEN clause:

FROM Product product ORDER BY CASE WHEN product.supplier IS NULL THEN 0 ELSE product.supplier.firstName END

So this worked fine while I used mysql 5.0.X.

Currently I am using newer versions of mysql (5.5.X) - and now provided above example DOESN'T WORK as it should be --- AGAIN products with the supplier = null, doesn't available in the result List...

I'm kindly asking, to share with me the way you used to go with in such situations - when you are going to order by the result by any property of the 'many-to-one' property which is NULLABLE.


Top
 Profile  
 
 Post subject: Re: Hibernate order by nullable 'many to one' property...
PostPosted: Sat Sep 01, 2012 8:56 am 
Newbie

Joined: Thu Aug 30, 2012 9:07 am
Posts: 3
Hey, nobody has something to share regarding this problem?

Any help would be GREATLY appreciated.


Top
 Profile  
 
 Post subject: Re: Hobernate order by 'many to one' property...
PostPosted: Sun Sep 02, 2012 9:47 am 
Newbie

Joined: Wed Sep 21, 2011 2:20 pm
Posts: 16
I think you are posting this in the wrong area here in the forum.

but nevertheless have a look at "outer joins". they probably do, what you are looking for.


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