“不存在具有给定标识符的行”尽管它确实存在 [英] "No row with the given identifier exists" although it DOES exist
问题描述
我使用Hibernate并获取
线程main中的异常org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[#271]
这个错误的奇怪之处在于,具有给定标识的对象存在于数据库中。我在应用程序的另一个运行中插入了有问题的记录。如果我在同一次运行(即同一个hibernate会话)中访问它,那么检索数据似乎没有问题。
仅仅因为它可能是映射的错误:
public class ProblemClass implements Persistent {
@ManyToOne(optional = false)
private MyDbObject myDbObject;
}
public class MyDbObject implements Persistent {
@OneToMany(mappedBy =myDbObject)
private List< ProblemClass> problemClasses;
@ManyToOne(可选= false)
私有ThirdClass thirdClass;
}
即使在哪里看,我也绝对不知道。任何提示高度赞赏!
只是澄清:
数据被插入到应用程序的另一个RUN中。这绝对是在数据库中,因为我可以在应用程序终止后通过SQL查询来查看它。 THAT之后,即再次启动应用程序时,我在数据库的FIRST查询中遇到了错误 - 不删除,不涉及回滚。
添加:
因为有人问,这里是获取数据的代码:
public List< ProblemClass> getProblemClasses(){
Query query = session.createQuery(from ProblemClass);
返回query.list();
$
为了使它完整,下面是插入它的通用代码(在获取应用程序的另一个RUN之前):
public void save(持久持久性){
session.saveOrUpdate持续性);
解决方案尤里卡,我找到了!
问题如下:
表中的数据 ThirdClass code>没有正确保存。由于此数据是通过
可选= false
$ p从MyDbObject引用的$ p>
Hibernate创建了一个内部联接,从而为联接返回一个空的结果。因为数据在一个会话中执行(在缓存中我猜),所以没有任何问题。
MySQL不强制执行外键完整性,因此不会抱怨插入损坏的数据。
解决方案:可选= true或正确插入数据。
I am using Hibernate and getting
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [#271]
What is pretty weird about this error is, that the object with the given id exists in the database. I inserted the problematic record in another run of the application. If I access it in the same run (i.e. same hibernate session) there seem to be no problems retrieving the data.
Just because it could be a fault of the mapping:
public class ProblemClass implements Persistent {
@ManyToOne(optional = false)
private MyDbObject myDbObject;
}
public class MyDbObject implements Persistent {
@OneToMany(mappedBy = "myDbObject")
private List<ProblemClass> problemClasses;
@ManyToOne(optional = false)
private ThirdClass thirdClass;
}
I have absolutely no clue even where to look at. Any hints highly appreciated!
Just to clarify:
The data was inserted in another RUN of the application. It is definitely in the database, as I can see it via an SQL-Query after the application terminated. And after THAT, i.e. when starting the application again, I get the error in the FIRST query of the database -- no deletion, no rollback involved.
Addition:
Because it was asked, here is the code to fetch the data:
public List<ProblemClass> getProblemClasses() {
Query query = session.createQuery("from ProblemClass");
return query.list();
}
And just to make it complete, here is the generic code to insert it (before fetching in another RUN of the application):
public void save(Persistent persistent) {
session.saveOrUpdate(persistent);
}
解决方案 Eureka, I found it!
The problem was the following:
The data in the table ThirdClass
was not persisted correctly. Since this data was referenced from MyDbObject via
optional = false
Hibernate made an inner join, thus returning an empty result for the join. Because the data was there if executed in one session (in the cache I guess), that made no problems.
MySQL does not enforce foreign key integrity, thus not complaining upon insertion of corrupt data.
Solution: optional = true or correct insertion of the data.
这篇关于“不存在具有给定标识符的行”尽管它确实存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!