-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria expression fails
PostPosted: Tue Sep 06, 2005 10:02 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
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 ... AskForHelp

Hibernate 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:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 2:38 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I'm not sure if Criteria supports joining between two unrelated tables. I know it can be set up to join between two related classes that have a uni-directional or bi-directional relationship or proxies already set up in the hbm files. If so, that can be set up via the criteria.setFetchMode(args) method or the chaining of createCriteria methods ontop of each other such as

Code:
List results = createCriteria(Item.class).add(Expression.like("description","gc","MatchMode.ANYWHERE).createCriteria(Bid.class).etc etc etc


I'm not entirely positive if the above would work between two unrelated classes or how you would set a Expression.eq(args) method to work between the two.

I'd recomment using hql queries and named queries for simplicity's sake. The criteria seem to be more useful for queries with a large number of options that can be configured by UI interaction.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 4:36 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
Thanks for replying. I looked into this uni- bi-directional matter. I appears if I want to use 2 tables that refer to one an other I need to define a bidirectional relationship between the tables.
Normally in SQL we just define a fk that references to other tables pk. But in hibernate the referenced table has to know it has many "children" in the table that refers to it.

Before I had just a <many-to-one> mapping in workmate referring to pk of domain.
Now I added a <set one-to-many> mapping in the domain referring to children in workmate (workmates).

Now the criteria expression sees the second table. That is one problem fixed. ;)

is there a way to print out the generated sql statement before I do
wm.list(); (so before it is executed) ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 9:24 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
in your hibernate config file put

Code:
<prop key="hibernate.show_sql">true</prop>


and the sql will show up in your log file.


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