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?