2
我目前正在研究一些遺留代碼,所以我無法更改database schema或域模型。休眠映射xml多對多數組
的Java對象是這樣的:
我想創建一個映射,這將允許我執行下面的查詢,所以它返回與他們的接觸陣列中接觸的人員名單:
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>
如果有信息丟失,請詢問。
- 我們使用Hibernate 3.6.1
在此先感謝
感謝您的答覆。當我實現你的解決方案時,我得到以下錯誤:'元素類型的內容'連接'必須匹配'(子選擇?,註釋?,鍵,(屬性|多對一|組件|動態組件|任何) SQL插入?,SQL更新?,SQL-刪除?)」。 \t Person.hbm.xml'因此看起來數組標籤在連接標籤內無效。我們使用hibernate 3.6.1,我應該使用更新的版本嗎? – areander
對不起,我不知道。我用NHibernate測試了這個,所以我認爲hibernate也可以做到這一點。 – Firo
沒問題,感謝您花時間在NHibernate中測試它 – areander