Article Directory
Cause
When using Hibernate to implement one-to-many association mapping, there is a User class and an Order class.
The user class and the order class have a one-to-many relationship. There is a List attribute in the User class to store the order information corresponding to the user.
public class User {
private Integer id;
private String userName;
private String password;
private List orderList = new ArrayList<>()
}
An error encountered when Hibernate is used to set the orderly storage of the same user's orders through the <List> tag.
<list name="orderList" cascade="delete">
<key column="USER_ID"/>
<index column="index11"/>
<one-to-many class="Order"/>
</list>
Hibernate:
select
user0_.id as id1_1_0_,
user0_.name as name2_1_0_,
user0_.password as password3_1_0_
from
hibernate_05_02_user user0_
where
user0_.id=?
User [id=3, userName=李四, password=77777]
Hibernate:
select
orderlist0_.USER_ID as USER_ID3_0_0_,
orderlist0_.id as id1_0_0_,
orderlist0_.index11 as index4_0_,
orderlist0_.id as id1_0_1_,
orderlist0_.price as price2_0_1_
from
hibernate_05_02_orders orderlist0_
where
orderlist0_.USER_ID=?
Exception in thread "main" org.hibernate.HibernateException: null index column for collection: com.hibernate.entity.User.orderList
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:847)
at org.hibernate.collection.internal.PersistentList.readFrom(PersistentList.java:372)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.finishUpRow(CollectionReferenceInitializerImpl.java:77)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:121)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2183)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:565)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:392)
at org.hibernate.collection.internal.PersistentList.add(PersistentList.java:151)
at com.hibernate.ui.Test.addOrder(Test.java:62)
at com.hibernate.ui.Test.main(Test.java:18)
the reason
When Hibernate judges the index of the newly inserted order data of User A, it will first query the database for the largest index of User A’s existing List attributes. If User A places an order for the first time, query it in the Order table. If user A's order data is not available, a new Order data will be created automatically, and the default value is set to 0 in the corner column. If user A is not placing an order for the first time, Hibernate will query the previous data of user A in the Order table. If you can’t find the number of corner labels, you can’t judge how long the corner labels of the new data should be, and this error will be reported.
Solution
In the Order database table, user A’s last order tag can be manually set to a number.