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