0

在以下场景中,对于多对多关联,传递给getHibernateTemplate().find()方法的正确查询是什么,以便获取具有参与者的事件的事件(和人员)列表?

    _____________        __________________
   |             |      |                  |       _____________
   |   EVENTS    |      |   PERSON_EVENT   |      |             |
   |_____________|      |__________________|      |    PERSON   |
   |             |      |                  |      |_____________|
   | *EVENT_ID   | <--> | *EVENT_ID        |      |             |
   |  EVENT_DATE |      | *PERSON_ID       | <--> | *PERSON_ID  |
   |  TITLE      |      |__________________|      |  AGE        |
   |_____________|                                |  FIRSTNAME  |
                                                  |  LASTNAME   |
                                                  |_____________|



<class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
        <generator class="native"/>
    </id>   
    <property name="date" type="timestamp" column="EVENT_DATE"/>
    <property name="title"/>

    <set name="participants" table="PERSON_EVENT">
        <key column="EVENT_ID"/>
        <many-to-many column="PERSON_ID" class="Person"/>
    </set>      
</class>


<class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="native"/>
    </id>
    <property name="age"/>
    <property name="firstname"/>
    <property name="lastname"/>

    <set name="events" table="PERSON_EVENT" inverse="true">
        <key column="PERSON_ID"/>
        <many-to-many column="EVENT_ID" class="Event"/>
    </set>

</class>

像这样的几次尝试都失败了,但有以下例外:

getHibernateTemplate().find("from Event event where event.participants != null");

org.springframework.dao.InvalidDataAccessResourceUsageException 等等等等嵌套异常是 org.hibernate.exception.SQLGrammarException: could not execute query etc etc

谢谢!

4

2 回答 2

0

您可以使用以下查询来获得相同的结果。

来自 event.participants 不为空的 Event 事件

于 2012-04-13T12:35:47.907 回答
0

Ram Reddy 建议的查询有效,但我需要添加lazy="false":

<set name="participants" table="PERSON_EVENT" lazy="false">
        <key column="EVENT_ID"/>
        <many-to-many column="PERSON_ID" class="Person"/>
</set>  
于 2012-04-16T11:01:16.473 回答