I'm having problems with a Hibernate criteria. I'm trying to make a Criteria where I look at the id of a member object of the class the query returns.
For example:
Criteria crit = session.createCriteria(Enquiry.class);
crit.add(Expression.eq("lecture.admin.id", userId));`
The result of this is an exception:org.hibernate.QueryException: could not resolve property: lecture.admin.id of: xxx.yyy.Enquiry
The Enquiry
class does contain the lecture variable, which in turn contains the admin variable. I have tried using lecture.id
and that works fine.
Is there a limit to the number of levels you can go down the object hierarchy like this?
Thanks!
Code snippets:
public class Lecture extends TransferItem {
private User admin;
public User getAdmin() {
return admin;
}
}
The 'User' class extends the Person
class, which in turn extends an Item
class, which has the getId()
method:
public Integer getId() {
if (id != null) {
return id;
}
return TransferBean.NOT_SET;
}
From the Hibernate mapping XML:
<class name="User" table="user">
<id column="user_id" name="id">
<generator class="increment"/>
</id>
...
<class name="Lecture" table="lecture">
<many-to-one class="User" column="user_fk" lazy="false" name="admin"/>`
This is the user
table:
mysql> show columns from user;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| user_id | int(11) | NO | PRI | | |
| firstname | varchar(50) | YES | | NULL | |
| lastname | varchar(50) | YES | | NULL | |
| signature | varchar(16) | YES | | NULL | |
| email_signature | varchar(256) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| phone | varchar(16) | YES | | NULL | |
| email | varchar(255) | YES | UNI | NULL | |
| lecturer_fk | int(11) | YES | MUL | NULL | |
| access | int(11) | YES | | NULL | |
| deleted | tinyint(1) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
11 rows in set (0.02 sec)`
You can't use nested paths directly in Criteria API (unlike HQL). Instead, you need to create nested criteria instances or define aliases on each "entity.property" pair starting with the first non-root entity:
Criteria criteria = session.createCriteria(Enquiry.class)
.createAlias("lecture", "l")
.createAlias("l.admin", "a")
.add( Restrictions.eqProperty("a.id", userId) );
Note that the very first property is not prefixed as it belongs to the root entity (Enquiry
), the others are prefixed with previous level alias. Details are in the documentation.
Also note that id
is a special property when it comes to associations; in your case user_fk
is a column located in lecture
table. It should, therefore, be possible (provided that the mappings you've posted are accurate) to rewrite the above criteria as:
Criteria criteria = session.createCriteria(Enquiry.class)
.createAlias("lecture", "l")
.add( Restrictions.eqProperty("l.admin.id", userId) );
thus eliminating extra join.
Criteria crit = session.createCriteria(Enquiry.class)
crit.createAlias("lecture.admin", "lectureAdmin");
crit.add(Expression.eq("lectureAdmin.id", userId));
You can indeed hit issues when you go down too deep in the object graph. I usually get around this by creating an alias as shown above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With