Hi all,
First of all this is my first post on this forum ;)
I'm working on genereating a criteria that will return a list of data from two joined tables.
The tables involved are:
Domain (pk id, fk ppo_id, name)
Workmate (pk id, fk domain_id, fk ppo_id, name, ...)
So I need a statement like this:
Code:
select this.ID as ID0_, this.PPO_ID as PPO_ID0_, this.DOMAIN_ID as DOMAIN_ID0_, this.FIRSTNAME as FIRSTNAME0_from WORKMATE this inner join Domain that on this.DOMAIN_ID = that.idwhere (this.PPO_ID=24) and that.NAME like '%'
this statement will vary depending on input filters
(domain name, search string)
I'm using criteria to handle this.
Code:
Criteria dom = session.createCriteria(Domain.class);
if (!workmate.getDomainId().equals(new Integer(0))) {
dom.add( Expression.like( "id", workmate.getDomainId().toString()));
} else if (workmate.getDomainId().equals(new Integer(0))) {
dom.add( Expression.like( "id", "'%'"));
}
if(!search.equalsIgnoreCase("")) {
dom.add( Expression.like("name", search, MatchMode.ANYWHERE));
} else {
dom.add( Expression.like("name", "'%'", MatchMode.ANYWHERE));
}
Criteria wm = dom.createCriteria("workmate");
if (!search.equalsIgnoreCase("")) {
wm.add( Expression.like( "firstname", search, MatchMode.ANYWHERE));
wm.add( Expression.like( "lastname", search, MatchMode.ANYWHERE));
}
if (workmate.getIsActive() != null) {
wm.add( Expression.like( "isactive" , workmate.getIsActive(), MatchMode.ANYWHERE));
}
wm.list();
It quits when I create second criteria adding workmate to the equasion. I think this might be happening because the pk name and fk name from respective tables are not equal. Is this the case?
If so, how can I add the createria saying it should look at the id as a key to join. Or am I missing something else?
Or should I just use Query to generate the sql?
Any help would be appreciated in this matter.
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelpHibernate version: 2.1.8 Mapping documents:Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using:The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code: