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? **** 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; } } **** 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( at org.hibernate.collection.PersistentList.readFrom( at org.hibernate.loader.Loader.readCollectionElement( at org.hibernate.loader.Loader.readCollectionElements( at org.hibernate.loader.Loader.getRowFromResultSet( at org.hibernate.loader.Loader.doQuery( at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections( at org.hibernate.loader.Loader.loadCollection( at org.hibernate.loader.collection.CollectionLoader.initialize( at org.hibernate.persister.collection.AbstractCollectionPersister.initialize( at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection( at org.hibernate.impl.SessionImpl.initializeCollection( at org.hibernate.collection.AbstractPersistentCollection.forceInitialization( at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections( at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections( at org.hibernate.loader.Loader.doList( at org.hibernate.loader.Loader.listIgnoreQueryCache( at org.hibernate.loader.Loader.list( at org.hibernate.loader.criteria.CriteriaLoader.list( at org.hibernate.impl.SessionImpl.list( at org.hibernate.impl.CriteriaImpl.list( at org.hibernate.impl.CriteriaImpl.uniqueResult( at com.nadhi.list.test.Main.getPerson( at com.nadhi.list.test.Main.main( 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 答疑

评论 抢沙发

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

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