Using multiple lists in Hibernate

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
homeAddresses = new ArrayList
(); private List
officeAddresses = new ArrayList
(); 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
getHomeAddresses() { return homeAddresses; } public void setHomeAddresses(List
L) { homeAddresses = L; } public List
getOfficeAddresses() { return officeAddresses; } public void setOfficeAddresses(List
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:** **Address.hbm.xml:** 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?

以上就是Using multiple lists in Hibernate的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏