-->
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.  [ 2 posts ] 
Author Message
 Post subject: a set contains an object expression in Criteria API
PostPosted: Thu Oct 13, 2005 7:21 am 
Newbie

Joined: Wed Jun 22, 2005 5:25 am
Posts: 6
Hello,

I have a Song class and a Value class who have a many-to-many relationship between them (through the table songvalue).

Now i want to select all songs that contain a certain value object with the criterion api. i can't just use a bidirectional binding, by just doing value.getSongs() or something, because i need it as a Criterion in an algorithm to create one bigger query...

To give an example on what i want to achieve i wrote this HQL query:

Code:
Query qvs = DBConnectionManager.currentSession().createQuery(
                "from Song song where :val member of song.values");
qvs.setParameter("val", val);



This works correct. but i need a criterion object... The next code DOES NOT WORK (because there is no contains method), but just as an example of something im looking for:

Code:
Criteria crit = HibernateUtil.currentSession().createCriteria(Song.class);
crit.add(Restrictions.contains("values", val);



does anyone know how to express something like this in the Criteria API?

Hibernate version:
3.0.1

Mapping documents:

Code:
<class name="datamonkey.hibernate.Value" table="values" schema="public">

   <id name="valueId" type="integer">
       <column name="value_id" not-null="true" unique="true" sql-type="integer" />
       <generator class="sequence">
          <param name="sequence">values_value_id_seq</param>
       </generator>
    </id>   
      
   <many-to-one name="parameter" class="datamonkey.hibernate.Parameter">
        <column name="param_id" not-null="true" />
    </many-to-one>
   
    <property name="valueName" type="java.lang.String">
        <column name="valuename" not-null="true" sql-type="varchar" />
   </property>
   
   <set name="songs" table="songvalue" inverse="true">
       <key column="value_id"/>
       <many-to-many column="song_id" class="datamonkey.hibernate.Song"/>
   </set>   
      
</class>


<class name="datamonkey.hibernate.Song" table="songs" schema="public">
   
    <id name="songId" type="integer">
       <column name="song_id" not-null="true" unique="true" sql-type="integer" />
       <generator class="sequence">
          <param name="sequence">songs_song_id_seq</param>
       </generator>
    </id>
   
   <set name="values" table="songvalue">
      <key column="song_id"/>
      <many-to-many column="value_id" class="datamonkey.hibernate.Value"/>
   </set>
   
</class>


</hibernate-mapping>



Name and version of the database you are using:

PostgreSQL 8.0.3

The generated SQL (show_sql=true):

This is the sql query that has been generated out of the HQL query:

Code:
12:44:31,703 DEBUG SQL:324 - select song0_.song_id as song1_ from public.songs song0_ where ? in (select value2_.value_id from songvalue values1_, public.values value2_ where song0_.song_id=values1_.song_id and values1_.value_id=value2_.value_id)


Top
 Profile  
 
 Post subject: never mind
PostPosted: Thu Oct 13, 2005 9:17 am 
Newbie

Joined: Wed Jun 22, 2005 5:25 am
Posts: 6
i found it!

i used an SQLCriterion.

Code:
Criterion c = Restrictions.sqlRestriction("{alias}.song_id in (select s2.song_id from songs s2, songvalue sv where s2.song_id = sv.song_id and sv.value_id = ?)", val.getValueId(), Hibernate.INTEGER);



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