hibernate - query with criteria on associated objects -


i've been struggling hibernate query problem , i'm hopeful can nudge in right direction.

i have object named flashcard has associated objects named tags. flashcard can have 1 or more tags (kind of how stackoverflow question can assigned multiple tags).

i'm trying use hibernate return flashcards assigned specific tags. example, query flashcards have both "tag2" , "tag4".

i've tried approach criteria queries, example queries , hql without luck far.

here's database looks like: mysql> select * flashcard;

+--------------+------------+----------+ | flashcard_id | question   | answer   | +--------------+------------+----------+ |            1 | question 1 | answer 1 | |            2 | question 2 | answer 2 | |            3 | question 3 | answer 3 | |            4 | question 4 | answer 4 | +--------------+------------+----------+ 4 rows in set (0.00 sec)  mysql> select * tag; +--------+------+ | tag_id | name | +--------+------+ |      1 | tag1 | |      3 | tag2 | |      2 | tag3 | |      4 | tag4 | |      5 | tag5 | +--------+------+ 5 rows in set (0.00 sec)  mysql> select * flashcard_tags; +--------+--------------+ | tag_id | flashcard_id | +--------+--------------+ |      2 |            1 | |      3 |            1 | |      4 |            1 | |      3 |            2 | |      4 |            2 | |      5 |            2 | |      3 |            3 | +--------+--------------+ 7 rows in set (0.00 sec) 

using data above, can see flashcard's 1 , 2 have both "tag2" , "tag4".

i created earlier post confirm sql (actual sql) return results , here's 2 working examples.

example1 of working sql query:

select  f.*    (         select  flashcard_id            tags t         join    flashcard_tags ft         on      ft.tag_id = t.tag_id           t.name in ('tag2', 'tag4')         group                 flashcard_id         having  count(*) = 2         ) ft join    flashcard f on      f.flashcard_id = ft.flashcard_id 

example 2 of working sql query:

select f.* flashcard f   inner join flashcard_tags ft1 on f.flashcard_id = ft1.flashcard_id   inner join tag t1 on ft1.tag_id = t1.tag_id , t1.name = 'tag2'   inner join flashcard_tags ft2 on f.flashcard_id = ft2.flashcard_id   inner join tag t2 on ft2.tag_id = t2.tag_id , t2.name = 'tag4' 

here's hibernate mappings like:

<hibernate-mapping>    <class name="org.robbins.flashcards.model.flashcard" table="flashcard">       <id name="flashcardid" type="int" column="flashcard_id">          <meta attribute="scope-set">public</meta>          <generator class="native" />       </id>       <property name="question" type="string">          <meta attribute="use-in-tostring">true</meta>          <column name="question" not-null="true" unique="true" />       </property>       <property name="answer" type="text">          <meta attribute="use-in-tostring">true</meta>          <column name="answer" not-null="true" />       </property>       <set name="tags" table="flashcard_tags">          <meta attribute="field-description">tags flashcard</meta>          <key column="flashcard_id" />          <many-to-many class="org.robbins.flashcards.model.tag"             column="tag_id" />       </set>    </class> </hibernate-mapping>  <hibernate-mapping>    <class name="org.robbins.flashcards.model.tag" table="tag">       <id name="tagid" type="int" column="tag_id">          <meta attribute="scope-set">public</meta>          <generator class="native" />       </id>       <property name="name" type="string">          <meta attribute="use-in-tostring">true</meta>          <column name="name" not-null="true" unique="true" />       </property>       <set name="flashcards" table="flashcard_tags" inverse="true">          <meta attribute="field-description">flashcards tag</meta>          <key column="tag_id" />          <many-to-many class="org.robbins.flashcards.model.flashcard"             column="flashcard_id" />       </set>    </class> </hibernate-mapping> 

lastly, here's excerpt generated flashcard class can see associated tags:

public class flashcard  implements java.io.serializable {      private int flashcardid;      private string question;      private string answer;      private set<tag> tags = new hashset<tag>(0); } 

i've run problems creating hibernate code retrieve same result. tried criteria query learned not support sql "having" clause or using "subquery" in clause sql example #1 does.

i tried querying example (passing in flashcard example had 2 tag objects attached) , received no results @ all.

i tried looking hql read "that hql subqueries can occur in select or clauses."

rather make post longer, i'll refrain posting examples of failed hibernate code. i'll gladly post them if helpful.

any assistance appreciated. thanks!

look @ article, explains how use hql many many relationship.

http://www.sergiy.ca/how-to-write-many-to-many-search-queries-in-mysql-and-hibernate/


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -