Jpa ejbql按列表属性查询

时间:2012-05-18 16:36:41

标签: java hibernate jpa ejbql

我有对象:

PersenInfo : (long id,String name, String lastName,String email)
AnsMsg : (long id,PersenInfo fromPersoninfo,String theMsg)
Person : (PersonInfo personInfo, List<AnsMsg> msgs)

person.hbm看起来像这样:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 4, 2012 2:48:41 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="Entities.Person" table="PERSON">
        <id name="id" type="long">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <many-to-one name="personInfo" class="Entities.PersonInfo" cascade="all" not-null="false" lazy="false"/>

        <property name="_pass" type="java.lang.String" access="field" lazy="false">
            <column name="_PASS" />
        </property>
        <property name="_IsConnect" type="int" access="field" lazy="false">
            <column name="_ISCONNECT" />
        </property>
        <property name="c2dmId" type="java.lang.String" lazy="false">
            <column name="C2DMID" />
        </property>

        <property name="lastSeen" type="long" lazy="false">
            <column name="LASTSEEN" />
        </property>
        <list name="msgList" inverse="false" table="ANSMSG" lazy="false" >
             <key column="parent_id" />
              <index column="idx"  />
            <one-to-many class="msg.AnsMsg"  />
        </list>
    </class>
</hibernate-mapping>

现在我尝试获取fromPerson = 6的所有AnsMsg列表以及parent_id = 3的所有AnsMsg;

我的查询应该如何? ( 我的意思是这样的:

"from AnsMsg ansMsg where ansMsg.fromPerson.id='6' or ansMsg.parent_id='6'" 

当然这个查询没有假设工作.. )

这与我的数据相似:

Db example

提前感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

select m.* from Person p, p.msgList m, m.fromPersonInfo f where p.id = 6 or f.id = 6
  1. 确定父ID(收件人,我猜)加入Person和AnsMsg表。
  2. 某人发送的邮件使用m.fromPersonInfo.id进行了审核,但JPA无法评估此类表达式,因此必须分离到m.fromPersonInfo ff.id = 6
  3. Obs。:如果您使用Person和PersonInfo实例作为查询的参数,您应该这样做:select m.* from Person p, p.msgList m where p = :person or m.fromPersonInfo = :fromPersonInfo