2

我目前正在处理一些遗留代码,因此我无法更改数据库架构或域模型。

Java 对象如下所示:

class Person {
  private int prsnId; //get, set
  private int custId; //get, set
  private String name; //get, set
  private String mail; //get, set
  private String status; //get, set
  private String logon; //get, set
  private Contact[] contact; //get, set, add
}

class Contact {
  private int cntcId; //get, set
  private int prorId; //get, set
  private String contactType; //get, set
  private String contactLanguage; //get, set
  private String contactUser; //get, set
}

我正在尝试创建一个映射,该映射将允许我执行以下查询,因此它返回一个人员列表,其中包含联系人数组中的联系人:

List<Person> personList = new ArrayList<Person>();
int customerId = /*something*/;
DetachedCriteria subQuery = DetachedCriteria.forClass(PersonOrg.class, "persorg")
.setProjection(Projections.projectionList().add(Projections.property("persorg.prsnId")))
.add(Restrictions.eq("custId", customerId));
Criteria criteria = session.createCriteria(Person.class);
criteria.setFetchMode("Person", FetchMode.JOIN)
    .setFirstResult(0)
    .setMaxResults(20)
    .add(Property.forName("prsnId").in(subQuery));          
personList = criteria.list();

通过我现在拥有的映射,我得到了一个人员列表,但他们的联系人数组填充了空值。我不清楚为什么。到目前为止,我的映射是:

<hibernate-mapping>
    <class name="be.bene.cris2.protocol.Person" table="BENE_CUST_PERSON" dynamic-update="true" dynamic-insert="true">
        <id name="prsnId" type="int">
            <column name="PRSN_ID" precision="10" scale="0" />
             <generator class="sequence">
                   <param name="sequence">CUST_PROR_SEQ</param>
            </generator>
        </id>

        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property> 

        ...

        <array name="contact" table="BENE_CUST_PERSORG" inverse="true" fetch="join">
            <key column="PRSN_ID" not-null="false"/>
            <index column="CUST_ID"/>
            <many-to-many entity-name="be.bene.cris2.protocol.Contact" column="PROR_ID" not-found="ignore"/>
        </array>

        <join table="BENE_CUST_PERSORG">
            <key column="PRSN_ID"/>
            <property name="custId" column="CUST_ID"/>
            <property name="prorId" column="PROR_ID"/>
            <property name="persorgType" type="be.bene.cris2.usertypes.CustomMasterSecundaryTertiaryIndicatorType" column="PERSORG_TYPE"/>
        </join>

    </class>
</hibernate-mapping>

如果缺少信息,请询问。

  • 我们使用休眠 3.6.1

提前致谢

4

1 回答 1

0
<join table="BENE_CUST_PERSORG">
  <key column="PRSN_ID"/>
  <property name="custId" column="CUST_ID"/>
  <property name="prorId" column="PROR_ID"/>
  <property name="persorgType" type="be.bene.cris2.usertypes.CustomMasterSecundaryTertiaryIndicatorType" column="PERSORG_TYPE"/>
  <array name="contact" inverse="true">
    <key column="PROR_ID" not-null="false"/>
    <index column="<columninContactTablecontainingArrayIndex>"/> or <index formula="(SELECT ROW_NUMBER() FROM Contacts c WHERE <FK matches> )"/>
    <one-to-many entity-name="be.bene.cris2.protocol.Contact" not-found="ignore"/>
  </array>
</join>
于 2012-10-26T09:24:42.073 回答