-->
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.  [ 8 posts ] 
Author Message
 Post subject: HQL WITH analog using Criteria API(extra JOIN ON parameters)
PostPosted: Mon Jul 21, 2008 6:27 am 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.6 GA

Mapping documents: Annotations

Name and version of the database you are using: MS SQL 2005

I have two tables - Enitites and Profiles

Entities:
bigint id
varchar name

Profiles:
bigint entity_type
bigint entity_id
bigint role_id
bit allowed

Situation:
I use code like
Code:
@Entity
@Table(name = "entities")
public class Entity {
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name")
    private String name;
   
    @OneToMany
    @Where(clause = "entity_type=3")
    @JoinColumn(name = "entity_id")
    private List<Profile> profile;
}

@Entity
@Table(name = "profiles")
public class Profile {
    @Column(name = "entity_type")
    private Long entityType;

    @Column(name = "entity_id")
    private Long entityId;

    @Column(name = "allowed")
    private Boolean allowed;

    @Column(name = "role_id")
    private Long roleId;

    @Column(name = "name")
    private String name;
}

When i try to get entities which are accessible by profile i use such code:
Code:
            Criteria criteria2 = getSession().createCriteria(Entity.class);
            Criteria prf = criteria2.createCriteria("profile", "prf");
            prf.add(Restrictions.eq("prf.allowed", true));
            prf.add(Restrictions.in("prf.roleId", listOfRoleIds));
            List list3 = criteria2.list();

This generates SQL like
Code:
   select
      this_.id as id_01,
      this_.name as name_01
    from
        entities this_
    inner join
        profile prf1_
            on this_.id=prf1_.entity_id
            and (
                prf1_.entity_type=3
            ) 
   where prf1_.allowed = 1   and prf1_.role_id in (1,2,3,4)

This sql runs on my database near the 40 seconds.
But if i move conditions to on cause it will run in 110 milliseconds
Code:
     select
      this_.id as id_01,
      this_.name as name_01
    from
        entities this_
    inner join
        profile prf1_
            on this_.id=prf1_.entity_id
            and (
                prf1_.entity_type=3
            )  and (prf1_.allowed = 1   and prf1_.role_id in (1,2,3,4))

I need to specify such roles while creating the criteria - so i cannot use nither filters (as i understand) nor @Where cause.
Again - i cannot use HQL - all aplication use Criteria API.

Is it possible to implement functionality like HQL With using the Criteria API?


Last edited by Gorbush on Tue Aug 19, 2008 6:14 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 7:48 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Quote:
Is it possible to implement functionality like HQL With using the Criteria API?

Yes, It is possible. Any thing which you can do with HQL, you can also do with Criteria API.

Quote:
Criteria criteria2 = getSession().createCriteria(Entity.class);
Criteria prf = criteria2.createCriteria("profile", "prf");
prf.add(Restrictions.eq("prf.allowed", true));
prf.add(Restrictions.in("prf.roleId", listOfRoleIds));
List list3 = criteria2.list();



Replace it with following:
Criteria criteria2 = getSession().createCriteria(Entity.class);
Criteria prf = criteria2.createAlias("profile", "prf");
prf.add(Restrictions.eq("prf.allowed", true));
prf.add(Restrictions.in("prf.roleId", listOfRoleIds));
List list3 = criteria2.list();

or Try this one:
Criteria criteria2 = getSession().createCriteria(Entity.class);
prf.add(Restrictions.eq("criteria2.profile.allowed", true));
prf.add(Restrictions.in("criteria2.profile.roleId", listOfRoleIds));
List list3 = criteria2.list();

_________________
amer sohail


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 8:18 am 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
The first query renders the same SQL as i posted by number 1.

The second query renders exception
org.hibernate.QueryException: could not resolve property: profile.allowed of: Profile
- it required to create alias explicitly using createAlias or createCriteria scince Hibernate 3.2.2 as i know.

(I removed "criteria2." from th line 2 and 3 - it's unknown to this context.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 2:23 pm 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
I do not beleive that no one get in the same situation...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 1:08 pm 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
Any comments??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 2:12 pm 
Newbie

Joined: Mon Aug 18, 2008 10:19 am
Posts: 5
Gorbush,

Like you I am having a similar problem as I need criteria to build a dynamic list of where conditions. However, looking at the example above could you not solve this with HQL? Something like ....


FROM entities inner join profile
WITH entityType = :entityType
AND ( allowed = :allowed
AND roleId in : roleId)

Apologies if this is a stupid response


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 2:24 pm 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
I developed a big system with many classes with subclasses which add some restrictions to the query..
So - i cannot create plain HQL query...

the big problem that there is an issue that not solved scince year of 2004...
http://opensource.atlassian.com/projects/hibernate/browse/HHH-16


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 7:48 am 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
Ok,
I made some fixes in hibernate-core and there is a patch to support join ON conditions.

Look in JIRA ticket for the archive:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2308[/url]


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