-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria not working with many-to-many field
PostPosted: Wed Sep 19, 2007 5:25 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
Hi,

I wonder if and how it is possible to search for entites by defining associated attributes. I want to use Criteria queries.

Better to show an example. I have an entity like this with a many-to-many association "businesses":

Code:
@Entity
public class Asset extends Basic {

   // Fields
   @Id
   private Long id;

   @ManyToOne
   private Users validatedByUser;

   @ManyToMany(targetEntity = Business.class)
   @JoinTable(name = "ASSET_BUSINESSES", joinColumns = { @JoinColumn(name = "ASSETID") }, inverseJoinColumns = { @JoinColumn(name = "BUSINESSID") })   
   private List<Business> businesses;

// geters setters


There is a join table "asset_businesses". I tried to use Criteria like this:

Code:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Asset.class);
criteria.add(Restrictions.eq("businesses", listOfBusinesses));
criteria.list();


Code:
Unfortunately I get an Exception: java.sql.SQLException: Missing IN or OUT parameter at index:: 1


I want to get all Asset entities that are associated with the businesses I provided.

What other way is there to achieve this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 4:07 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
Isn't there a proper way to query many to many fields within a Criteria?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 3:50 pm 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
I found an SQL statement that works for me. What would the corresponding Criteria-query look like? Please?

Code:
SELECT * FROM asset WHERE asset.id IN (

  SELECT asset.id FROM asset asset
  JOIN asset_businesses abs ON asset.id=abs.assetid AND (abs.businessid=1 OR abs.businessid=4)
  HAVING COUNT( DISTINCT abs.businessid ) = 2 GROUP BY id
     
);


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