public class Person{
private UserInfo userInfo;
//Getters and setters omitted
}
public class UserInfo {
private String username;
}
You want to load a Person object by setting a restriction on the username property of the UserInfo class. So at first you might try something like this:
Criteria personCriteria = getCurrentSession().createCriteria(Person.class)
.add(Restrictions.ilike("userInfo.username", "someUserNameGoesHere"));
What happens when you do that is hibernate throws up all over itself. Here is the way to make it work:
Criteria countCriteria = getCurrentSession().createCriteria(Person.class)
.createAlias("userInfo", "ui")
.add(Restrictions.ilike("ui.username", "someUserNameGoesHere"));
So all that really needs to be done is create an alias for the UserInfo entity and reference that in your restriction statement, DONE!
Thank you !!
ReplyDeletejust found this tip after a hour of clueless googling, thank you!
ReplyDelete'hibernate throws up all over itself' is a perfect summary of this behavior
Well that was almost too easy, I thought I was going to have to pull a list of objects...luckily, I am lazy and nothing is every really too easy. Thanks!
ReplyDelete