-->
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: How select root of subclass without LEFT OUTHER JOIN
PostPosted: Fri Jun 08, 2012 7:05 am 
Newbie

Joined: Fri Jun 08, 2012 6:49 am
Posts: 2
Hi, I have a performance trouble (in real have 15 subclass). I have more subclass with InheritanceType.JOINED (common root table).

Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class FooEntity {
   @Id private int id;
}

@Entity
@PrimaryKeyJoinColumn(name = "id")
public class FooChild extends FooEntity { }


Now I need a query that selects the ID:

Code:
Criteria criteria = hibernate.getSessionFactory().getCurrentSession().createCriteria(FooEntity.class);
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("id"));
criteria.setProjection(properties);
criteria.list();


But I get this complex select:

Code:
    select
        this_.id as y0_
    from
        FooEntity this_
    left outer join
        FooChild this_1_
            on this_.id=this_1_.id


All entities are still in the table FooEntity. Why Hibernate add the "left outer join"? Just a simple select:

Code:
    select   this_.id as y0_  from  FooEntity this_


How do I remove the join?


Top
 Profile  
 
 Post subject: Re: How select root of subclass without LEFT OUTHER JOIN
PostPosted: Fri Jun 08, 2012 2:27 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
FooChild and FooEntity are two different entities.

FooChild is a child of FooEntity and another table will be created to hold the FooChild data.

Thus when hibernate constructs the SQL it has to do a join in order to provide the ability to select fields off of FooChild.


Top
 Profile  
 
 Post subject: Re: How select root of subclass without LEFT OUTHER JOIN
PostPosted: Mon Jun 11, 2012 5:16 am 
Newbie

Joined: Fri Jun 08, 2012 6:49 am
Posts: 2
reynolr wrote:
Thus when hibernate constructs the SQL it has to do a join in order to provide the ability to select fields off of FooChild.
Thank you for your reply. I understand why (in general) the Hibernate makes the JOIN. But I need only common column! I do not FooChild nor FooEntity. I need only ID from the Foo table.

So far the only solution: to use named query.
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@NamedQueries({ @NamedQuery(name = "allFooIds", query = "select id from FooEntity") })
public class FooEntity {
   @Id private int id;
}
It's important to specify the column. Use only "from FooEntity" leads to LEFT JOIN with FooChild!
Used this query is simple:
Code:
List<Integer> fooIds = hibernate.getSessionFactory().getCurrentSession().getNamedQuery("allFooIds");
Using HQL has the advantage that Hibernate monitors and performs the change table and column names.
But why Hibernate does not same change in Criteria API, it is a mystery to me.


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.