hibernate中关于报错 org.hibernate.HibernateException: null index column for collection: 异常的一个解决方法

文章目录


起因

在使用Hibernate实现一对多关联映射,有一个用户User类,一个Order订单类。
用户类与订单类是一对多的关系,User类中有一个List属性来存放用户对应的订单信息。

public class User {
    
    
	private Integer id;
	private String userName;
	private String password;
	private List orderList = new ArrayList<>()	
}

用Hibernate在通过<List>标签设置同一用户的订单有序存放时遇到的错误。

        <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)

原因

Hibernate在判断用户A的新插入的订单数据角标是多少时,会先在数据库中查询用户A已有的List属性的最大角标,如果用户A是第一次下单,在Order表中查询不到用户A的订单数据,则会自动创建一个新的Order数据,并且在角标列默认设置0,如果用户A不是第一次下单,Hibernate就会查询用户A在Order表的上一条数据的角标数,如果查询不到的话就无法判断新数据的角标应该时多少,就会报出这个错误。

解决方法

在Order数据库表中用户A的上一个订单角标手动设置为一个数字即可。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44627608/article/details/115050507