-->
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: Criteria API, nested references and InheritanceType.JOINED
PostPosted: Mon Mar 03, 2008 2:14 pm 
Newbie

Joined: Mon Oct 01, 2007 6:56 am
Posts: 6
Hi there,

I have a problem with ordering in a quite complex mapping. Here's the setup (simplified for brevity):

Code:
@Entity(name="content")
public class Content
{
    @Column
    private String externalAuthor;
}

@Entity(name="user")
public class User
{
    @Column
    private String name;
}

@Inheritance(strategy=InheritanceType.JOINED)
class AbstractArticle
{

    @OneToOne(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER) @JoinColumn(name="content_id")
    private Content content;

    @ManyToOne @JoinColumn(name="author_id")
    private User author;
}

@Entity(name="baseArticle")
@PrimaryKeyJoinColumn(name="article_id")
public class BaseArticle extends AbstractArticle
{
    // ...
}

@Entity(name="editedArticle")
@PrimaryKeyJoinColumn(name="article_id")
public class EditedArticle
extends AbstractArticle
{
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="base_article_id", nullable=true)
    private BaseArticle baseArticle;
    // ...
}


Everything works fine until I try to sort some EditedArticle Criteria results by baseArticle.content.externalAuthor or baseArticle.author.name, here's some code to make it clear:
Code:
Criteria search = session.createCriteria(EditedArticleBean.class);
// several eq() and isNull() restrictions skipped
// ...
// now we need to order the results by all possible author values, i.e.:
// baseArticle.content.externalAuthor, baseArticle.author.name, content.externalAuthor and author.name,
// the last two work fine using aliases:
search.createAlias("author", "author").addOrder(Order.asc("author.name"));
search.createAlias("content", "content").addOrder(Order.asc("content.externalAuthor"));
// the first two however require a "sub-query" which doesn't work out for some reason:
search = search.createCriteria("baseArticle");
search.createAlias("baseArticle.author", "baAuthor").addOrder(Order.asc("baAuthor.name"));
search.createAlias("baseArticle.content", "baContent").addOrder(Order.asc("baContent.externalAuthor"));


If I skip the last two lines everything works as expected, if I don't - I get an empty result set. The generated query is quite complex (involving a whole bunch of foo_a_b_ as bar_x_y_ and several [left] inner/outer joins) so I couldn't really figure out the main difference between a query with and without "sub-criterias"...
So... can anyone shed some light on this issue? What's the right way to use "order by" in nested associations and "joined" inheritance?

Any help is greatly appreciated!

TIA,
jenner


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 7:48 am 
Newbie

Joined: Mon Oct 01, 2007 6:56 am
Posts: 6
I found out why the result set in second case is empty: for some reason hibernate generates inner joins where left outer joins are expected, e.g.:
Code:
select this_.article_id as article1_0_5_, this_1_.archived as archived0_5_, this_1_.author_id as author5_0_5_, this_1_.content_id as content6_0_5_, this_1_.last_modified as last3_0_5_, this_1_.schema_name as schema4_0_5_, this_.approval_time_limit as approval2_2_5_, this_.author_approval as author3_2_5_, this_.base_article_id as base13_2_5_, this_.category_id as category10_2_5_, this_.comment_policy as comment4_2_5_, this_.editor_id as editor12_2_5_, this_.expiration_date as expiration5_2_5_, this_.magazine_id as magazine11_2_5_, this_.publication_date as publicat6_2_5_, this_.published as published2_5_, this_.superseded_article_id as superseded14_2_5_, this_.temporarily_offline_date as temporar8_2_5_, this_.temporarilyOffline as temporar9_2_5_, author2_.person_id as person1_10_0_, author2_.active as active10_0_, author2_.mail_address as mail3_10_0_, author2_.name as name10_0_, content1_.content_id as content1_13_1_, content1_.xml_data as xml2_13_1_, content1_.external_author as external3_13_1_, content1_.teaser as teaser13_1_, content1_.teaser_image_id as teaser5_13_1_, content1_.title as title13_1_, basearticl3_.article_id as article1_0_2_, basearticl3_1_.archived as archived0_2_, basearticl3_1_.author_id as author5_0_2_, basearticl3_1_.content_id as content6_0_2_, basearticl3_1_.last_modified as last3_0_2_, basearticl3_1_.schema_name as schema4_0_2_, basearticl3_.publicly_visible as publicly2_1_2_, basearticl3_.submission_date as submission3_1_2_, baauthor4_.person_id as person1_10_3_, baauthor4_.active as active10_3_, baauthor4_.mail_address as mail3_10_3_, baauthor4_.name as name10_3_, articlecon10_.content_id as content1_13_4_, articlecon10_.xml_data as xml2_13_4_, articlecon10_.external_author as external3_13_4_, articlecon10_.teaser as teaser13_4_, articlecon10_.teaser_image_id as teaser5_13_4_, articlecon10_.title as title13_4_ from edited_article this_

inner join article this_1_ on this_.article_id=this_1_.article_id
inner join person author2_ on this_1_.author_id=author2_.person_id
inner join content content1_ on this_1_.content_id=content1_.content_id
--inner join base_article basearticl3_ on this_.base_article_id=basearticl3_.article_id
left outer join base_article basearticl3_ on this_.base_article_id=basearticl3_.article_id
left outer join article basearticl3_1_ on basearticl3_.article_id=basearticl3_1_.article_id
--inner join person baauthor4_ on basearticl3_1_.author_id=baauthor4_.person_id
left outer join person baauthor4_ on basearticl3_1_.author_id=baauthor4_.person_id
left outer join content articlecon10_ on basearticl3_1_.content_id=articlecon10_.content_id

where this_.magazine_id=1 and this_.editor_id is null and this_.author_approval='accepted' and this_1_.archived='f' and this_.published='t' and this_.temporarilyOffline='f' order by content1_.external_author desc, author2_.name desc, baauthor4_.name asc limit 25;


I have replaced the two commented out lines (beginning with "-- inner join..") with "left outer join" and got the expected results.
The question is, why does hibernate generate inner joins here? The association to baseArticle is marked as nullable:
Code:
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="base_article_id", nullable=true)
    private BaseArticle baseArticle;


Shouldn't this force hibernate to create left outer joins?

cheers,
jenner


Top
 Profile  
 
 Post subject: Re: Criteria API, nested references and InheritanceType.JOINED
PostPosted: Thu Feb 20, 2014 6:37 am 
Newbie

Joined: Thu Feb 20, 2014 6:35 am
Posts: 1
Hey, does anybody found a solution for that? I have exactly same problem now....


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.