Reputation: 133
I have a Person
class mapped to a PERSON
table, and an Address
class mapped to an ADDRESS
table. What I want is for each Person
to have two lists of Address
es: homeAddresses
and officeAddresses
. The ADDRESS
table has one foreign-key field, PERSONID
, for both cases, but it has two separate list-index fields: a home_add_idx
for records belonging in homeAddresses
, and an off_add_idx
for records belonging in officeAddresses
. For any given ADDRESS
record, one of these list-index fields will be NULL
. This all works perfectly on insert, but when I try to retrieve records, I get an exception, "null index column for collection". (Full stacktrace below.)
How can I do this?
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>
When I insert data, I insert a separate home and office address for the same Person. It inserts correctly. However, when I try to retrieve the Person object, I get the following exception:
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)
In the database, I can see that for home address, the office address column index is empty; and for office address, the home address column index is empty. But isn't that expected? What do I need to do in order to retrieve the data correctly?
Upvotes: 3
Views: 1969
Reputation: 183544
I think you need to replace the ADDRESS.PERSONID
foreign-key field with two separate foreign-key fields — one for home addresses, one for office addresses.
The list-index fields, you can leave as they are, or you can merge them into a single field, or you can remove them entirely and use a <bag>
instead of a <list>
mapping. (Obviously that last approach is only good if you don't care about the order of addresses within the list; I don't know whether you do.)
When you think about it — it makes sense that the list-index column doesn't accomplish what you want. That column is only used for lists (not sets and bags), so it would not be a very general-purpose way to indicate which collection a record belongs to. So it makes sense that it's only used to specify the list-index for a record that's already been determined to belong to the right collection.
Upvotes: 2