我有Person
类映射到PERSON
表,Address
类映射到ADDRESS
表。我想要的是每个Person
都有两个 Address
es:homeAddresses
和officeAddresses
的列表。对于这两种情况,ADDRESS
表都有一个外键字段PERSONID
,但它有两个单独的列表索引字段:home_add_idx
表示属于homeAddresses
的记录,和off_add_idx
属于officeAddresses
的记录。对于任何给定的ADDRESS
记录,其中一个列表索引字段将为NULL
。这一切都完全适用于插入,但是当我尝试检索记录时,我得到一个异常,“收集的空索引列”。 (下面的完整堆栈跟踪。)
我该怎么做?
Person.java:
public class Person implements Serializable {
private String personId;
private String personName;
private List<Address> homeAddresses = new ArrayList<Address>();
private List<Address> officeAddresses = new ArrayList<Address>();
public String getPersonId() { return personId; }
public void setPersonId(String s) { personId = s; }
public String getPersonName() { return personName; }
public void setPersonName(String s) { personName = s; }
public List<Address> getHomeAddresses() { return homeAddresses; }
public void setHomeAddresses(List<Address> L) { homeAddresses = L; }
public List<Address> getOfficeAddresses() { return officeAddresses; }
public void setOfficeAddresses(List<Address> L) { officeAddresses = L; }
}
Address.java:
public class Address implements Serializable {
private String id;
private String houseNo;
// [SNIP - street, city, country]
private String postalCode;
public String getId() { return id; }
public void setId(String s) { id = s; }
public String getHouseNo() { return houseNo; }
public void setHouseNo(String s) { houseNo = s; }
// [SNIP - getters for street, city, country]
public String getPostalCode() { return postalCode; }
public void setPostalCode(String s) { postalCode = s; }
}
Person.hbm.xml:
<hibernate-mapping>
<class name="com.nadhi.list.test.Person" table="PERSON">
<id name="personId" type="java.lang.String">
<column name="PERSONID" />
<generator class="assigned" />
</id>
<property name="personName" type="java.lang.String">
<column name="PERSONNAME" />
</property>
<list name="homeAddresses" inverse="false" table="ADDRESS" lazy="false" cascade="all">
<key>
<column name="PERSONID"/>
</key>
<list-index column="home_add_idx"></list-index>
<one-to-many class="com.nadhi.list.test.Address" />
</list>
<list name="officeAddresses" inverse="false" table="ADDRESS" lazy="false" cascade="all">
<key>
<column name="PERSONID"/>
</key>
<list-index column="off_add_idx"></list-index>
<one-to-many class="com.nadhi.list.test.Address" />
</list>
</class>
</hibernate-mapping>
Address.hbm.xml:
<hibernate-mapping>
<class name="com.nadhi.list.test.Address" table="ADDRESS">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="houseNo" type="java.lang.String">
<column name="HOUSENO" />
</property>
<!-- [SNIP - mappings for street, city, country] -->
<property name="postalCode" type="java.lang.String">
<column name="POSTALCODE" />
</property>
</class>
</hibernate-mapping>
当我插入数据时,我为同一个人插入一个单独的家庭和办公室地址。它插入正确。但是,当我尝试检索Person对象时,我得到以下异常:
org.hibernate.HibernateException: null index column for collection: com.nadhi.list.test.Person.officeAddresses
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:770)
at org.hibernate.collection.PersistentList.readFrom(PersistentList.java:402)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1156)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:774)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:622)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:479)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:279)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:369)
at com.nadhi.list.test.Main.getPerson(Main.java:113)
at com.nadhi.list.test.Main.main(Main.java:36)
在数据库中,我可以看到,对于家庭地址,办公室地址列索引为空;对于办公地址,家庭地址列索引为空。但这不是预期的吗?为了正确检索数据,我需要做什么?
答案 0 :(得分:2)
我认为您需要将ADDRESS.PERSONID
外键字段替换为两个单独的外键字段 - 一个用于家庭地址,一个用于办公室地址。
list-index字段,您可以保持原样,也可以将它们合并到一个字段中,或者您可以完全删除它们并使用<bag>
而不是<list>
映射。 (显然,如果您不关心列表中的地址顺序,那么最后一种方法才有用;我不知道您是否这样做。)
当你考虑它时 - 列表索引列没有达到你想要的效果。该列仅用于列表(不是集合和包),因此它不是一种非常通用的方式来指示记录属于哪个集合。因此有意义的是,它仅用于为已经确定属于正确集合的记录指定list-index。