Hibernate查询从三个相关表中获取记录

时间:2014-08-07 11:50:09

标签: java hibernate orm

我有三个对象USER,CONTACT和ACTION。

每个USER都有很多联系人,每个联系人都有很多动作

每个联系人和行动都有分配给他们的状态,例如20或60或...... 请查看数据模型。

要求是让CONTACT具有特定状态,或者获取其ACTION具有该特定状态的CONTACT。 例如。让我联系状态20,或联系人的行动状态为20

目前,我有以下查询,即检索状态为20的CONTACT,并且不考虑ACTION的状态

USER

public class User {
    private Integer userID;
    private String userFirstName;
    private String userLastName;
    private Set<Contact> contactSet = new HashSet<Contact>();
    private Set<Action> actionSet = new HashSet<Action>();
    private ContactCriteria contactCriteria;
    .
    .
    .
    }

联系

public class Contact implements Serializable {
    private Integer contactID;
    private Integer contactStatus = 0;
    private String  givenName;
    private String  familyName;
    private String  streetAddress;
    private Set<User> userSet = new HashSet<User>();
    private Set<Action> actionSet = new HashSet<Action>();
    .
    .
    .
    }

ACTION

public class Action implements Serializable {
    private Integer actionID;
    private Integer actionStatus;
    private User    user;
    private String  actionNote;
    private Contact contact;
    .
    .
    .
    }

以下是我的映射文件: User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.smallworks.model" schema="smallworksdb">
 <class name="User" table="USERACCOUNT">
  <id column="USER_ID" length="500" name="userID">
   <generator class="increment"/>
  </id>
  <property column="USER_FIRSTNAME" generated="never" lazy="false" length="100" name="userFirstName"/>
  <property column="USER_LASTNAME" generated="never" lazy="false"  length="100" name="userLastName"/>

  <set cascade="all" fetch="select" lazy="true" name="contactSet" sort="unsorted" table="USER_CONTACT">
   <key column="USER_ID"/>
   <many-to-many class="com.smallworks.model.Contact"
     column="CONTACT_ID" order-by="CONTACT_ID" unique="false"/>
  </set>

  <!-- one to many mapping with Action -->
  <set inverse="true" lazy="true" name="actionSet" sort="unsorted" order-by="ACTION_DUE_DATE" cascade="save-update">
   <key column="USER_ID"/>
   <one-to-many class="com.smallworks.model.Action"/>
  </set>

   <!-- one to one mapping with ContactCriteria -->
   <one-to-one name="contactCriteria" class="com.smallworks.model.ContactCriteria"
            cascade="save-update" lazy="false"></one-to-one>
   </class>
</hibernate-mapping>

Contact.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.smallworks.model" schema="smallworksdb">
 <class name="Contact" table="CONTACT">
  <id column="CONTACT_ID" length="500" name="contactID">
   <generator class="increment"/>
  </id>
  <property column="GIVEN_NAME" generated="never" lazy="false"
   length="100" name="givenName"/>
  <property column="FAMILY_NAME" generated="never" lazy="false"
   length="100" name="familyName"/>
  <property column="STREET_ADDRESS" generated="never" lazy="false"
   length="100" name="streetAddress"/>
  <property column="CONTACT_STATUS" generated="never" lazy="false"
   name="contactStatus" type="integer"/>

   <set inverse="true" lazy="false" name="userSet" sort="unsorted" table="USER_CONTACT">
    <key column="CONTACT_ID"/>
    <many-to-many class="com.smallworks.model.User" column="USER_ID" unique="false"/>
  </set>

  <!-- one to many mapping with Action -->
  <set inverse="true" lazy="true" name="actionSet" sort="unsorted" order-by="ACTION_DUE_DATE" cascade="save-update">
   <key column="CONTACT_ID"/>
   <one-to-many class="com.smallworks.model.Action"/>
  </set>
  </class>
</hibernate-mapping>

Action.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.smallworks.model" schema="smallworksdb">
 <class name="Action" table="ACTION">
  <id column="ACTION_ID" length="500" name="actionID">
   <generator class="increment"/>
  </id>
  <property column="ACTION_STATUS" generated="never" lazy="false"
   name="actionStatus" type="integer"/>
  <!-- many to one mapping with Contact -->
  <many-to-one cascade="save-update"
   class="com.smallworks.model.Contact" column="CONTACT_ID" lazy="false"
   name="contact" not-null="true" />
  <!-- many to one mapping with User  -->
  <many-to-one class="com.smallworks.model.User" column="USER_ID"
   lazy="false" name="user" not-null="true"/>
 </class>
</hibernate-mapping>

我现有的查询是:

Query query = session.createQuery("select distinct c FROM com.smallworks.model.User as u INNER JOIN u.contactSet as c WHERE u.userID=:userIDPara AND c.contactStatus in (:contactStatusPara)");
    query.setParameter("userIDPara", user.getUserID());
    query.setParameterList("contactStatusPara", statusList);
    contactList = query.list();

enter image description here

1 个答案:

答案 0 :(得分:1)

在c.actions上添加外部联接作为a.status上的OR限制。

select distinct c FROM com.smallworks.model.User as u INNER JOIN u.contactSet as c LEFT OUTER JOIN c.actionSet a WHERE u.userID=:userIDPara AND (c.contactStatus in (:contactStatusPara) OR a.actionStatus in (: actionStatusPara)