org.hibernate.ObjectNotFoundException: No row with the given identifier exists
2017-02-21 14:53
1326 查看
1、错误描述
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.you.hibernate.model.TStudentInfo#1]
at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:247)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:171)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:160)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
at org.hibernate.engine.internal.StatefulPersistenceContext.unproxyAndReassociate(StatefulPersistenceContext.java:667)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:90)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:946)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:925)
at com.you.hibernate.dao.StudentDao.deleteStuInfo(StudentDao.java:116)
at com.you.hibernate.dao.StudentDao.main(StudentDao.java:167)
2、错误原因
public static void deleteStuInfo(int id)
{
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
TStudentInfo student = (TStudentInfo)session.load(TStudentInfo.class, id);
session.delete(student);
session.getTransaction().commit();
System.out.println("删除成功!");
}
/**
* 方法描述:
* 创建作者:游海东
* 创建日期:2017-2-20
* 创建时间:下午5:20:14
* 方法名称:main
* 方法类型:StudentDao
* 返回类型:void
* @param args
*/
public static void main(String[] args)
{
int id = 1;
deleteStuInfo(id);
}
由于id=1的记录已经删除,再次调用删除方法,会报错
3、解决办法
(1)首先,在调用删除方法前需要按照ID进行查询一次
(2)然后,如果该ID的记录存在,才能调用删除方法
(3)最后,调用删除操作进行删除记录
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.you.hibernate.model.TStudentInfo#1]
at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:247)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:171)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:160)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
at org.hibernate.engine.internal.StatefulPersistenceContext.unproxyAndReassociate(StatefulPersistenceContext.java:667)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:90)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:946)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:925)
at com.you.hibernate.dao.StudentDao.deleteStuInfo(StudentDao.java:116)
at com.you.hibernate.dao.StudentDao.main(StudentDao.java:167)
2、错误原因
public static void deleteStuInfo(int id)
{
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
TStudentInfo student = (TStudentInfo)session.load(TStudentInfo.class, id);
session.delete(student);
session.getTransaction().commit();
System.out.println("删除成功!");
}
/**
* 方法描述:
* 创建作者:游海东
* 创建日期:2017-2-20
* 创建时间:下午5:20:14
* 方法名称:main
* 方法类型:StudentDao
* 返回类型:void
* @param args
*/
public static void main(String[] args)
{
int id = 1;
deleteStuInfo(id);
}
由于id=1的记录已经删除,再次调用删除方法,会报错
3、解决办法
(1)首先,在调用删除方法前需要按照ID进行查询一次
(2)然后,如果该ID的记录存在,才能调用删除方法
(3)最后,调用删除操作进行删除记录
相关文章推荐
- Hibernate报错:org.hibernate.ObjectNotFoundException: No row with the given identifier exists 解决办法
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.chint.model.bas.Vendor#
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [cn.facekee.cms.entity.CmsFansgroup#195]
- 【hibernate】错误org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- 【hibernate】关于org.hibernate.ObjectNotFoundException: No row with the given identifier exists 的快捷解决问题
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.gaidea.ptims.po
- (已解决)org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists .
- Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.xxx.xx.model.info.News#15]
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [cn.itcast.erp.auth.
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
- Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: 原因
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.zero.space.entity.TblJd#38]解决方法
- 关于org.hibernate.ObjectNotFoundException: No row with the given identifier exists 的快捷解决问题
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [xx]
- 【hibernate】org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists