Java “不存在具有给定标识符的行”,尽管它确实存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2847231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:34:48  来源:igfitidea点击:

"No row with the given identifier exists" although it DOES exist

javahibernate

提问by roesslerj

I am using Hibernate and getting

我正在使用 Hibernate 并获得

Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [#271]

线程“main” org.hibernate.ObjectNotFoundException 中的异常:不存在具有给定标识符的行:[#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.

这个错误很奇怪的是,具有给定 id 的对象存在于数据库中。我在应用程序的另一次运行中插入了有问题的记录。如果我在同一次运行(即同一个休眠会话)中访问它,检索数据似乎没有问题。

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.

澄清一下:数据被插入到应用程序的另一个 RUN 中。它肯定在数据库中,因为我可以在应用程序终止后通过 SQL 查询看到它。在那之后,即再次启动应用程序时,我在数据库的第一个查询中得到错误——没有删除,没有涉及回滚。

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

为了使它完整,这里是插入它的通用代码(在获取应用程序的另一个 RUN 之前):

public void save(Persistent persistent) {
    session.saveOrUpdate(persistent);
}

采纳答案by roesslerj

Eureka, I found it!

尤里卡,我找到了!

The problem was the following:

问题如下:

The data in the table ThirdClasswas not persisted correctly. Since this data was referenced from MyDbObject via

表中的数据ThirdClass未正确持久化。由于此数据是通过 MyDbObject 引用的

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.

Hibernate 进行了内部联接,从而为联接返回空结果。因为如果在一个会话中执行数据(我猜是在缓存中),那么数据就在那里,这没有问题。

MySQL does not enforce foreign key integrity, thus not complaining upon insertion of corrupt data.

MySQL 不强制执行外键完整性,因此在插入损坏的数据时不会抱怨。

Solution: optional = true or correct insertion of the data.

解决方案:optional = true 或正确插入数据。

回答by Maurice Perry

Sounds like your transaction inserting is rollbacked

听起来您的事务插入已回滚

回答by Sujee

Possible reasons:

可能的原因:

  1. The row was inserted by the first session, but transaction was not committed when second session tried to access it.
  2. First session is roll-backed due to some reason.
  1. 该行是由第一个会话插入的,但是当第二个会话尝试访问它时没有提交事务。
  2. 由于某种原因,第一个会话被回滚。

回答by Lalchand Mali

Please update your hibernateconfiguration file as given below:

请更新您的hibernate配置文件,如下所示:

property start tag name="hbm2ddl.auto" create/update property close tag

回答by Nagaraja JB

This might be your case, kindly check my answer on another post.

这可能是你的情况,请查看我在另一篇文章中的回答。

https://stackoverflow.com/a/40513787/6234057

https://stackoverflow.com/a/40513787/6234057

I had the same Hibernate exception.

我有同样的休眠异常。

After debugging for sometime, i realized that the issue is caused by the Orphan child records.

经过一段时间的调试,我意识到问题是由孤儿子记录引起的。

As many are complaining, when they search the record it exists. What i realized is that the issue is not because of the existence of the record but hibernate not finding it in the table, rather it is due to the Orphan child records.

The records which have reference to the non-existing parents!

正如许多人抱怨的那样,当他们搜索记录时,它就存在。我意识到问题不是因为记录的存在,而是因为休眠没有在表中找到它,而是由于孤儿记录。

参考不存在的父母的记录!

What i did is, find the Foreign Key references corresponding to the Table linked to the Bean.

我所做的是,找到与链接到 Bean 的表相对应的外键引用。

To find foreign key references in SQL developer

在 SQL developer 中查找外键引用

1.Save the below XML code into a file (fk_reference.xml)

1.将下面的XML代码保存到一个文件中(fk_reference.xml)

<items>
<item type="editor" node="TableNode" vertical="true">
<title><![CDATA[FK References]]></title>
<query>
    <sql>
        <![CDATA[select a.owner,
                        a.table_name,
                        a.constraint_name,
                        a.status
                 from   all_constraints a
                 where  a.constraint_type = 'R'
                        and exists(
                           select 1
                           from   all_constraints
                           where  constraint_name=a.r_constraint_name
                                  and constraint_type in ('P', 'U')
                                  and table_name = :OBJECT_NAME
                                  and owner = :OBJECT_OWNER)
                           order by table_name, constraint_name]]>
    </sql>
</query>
</item>

2.Add the USER DEFINED extension to SQL Developer

2.在SQL Developer中添加USER DEFINED扩展

To find the Orphan records in all referred tables

在所有引用的表中查找孤立记录

select * from CHILD_TABLE where FOREIGNKEY not in (select PRIMARYKEY from PARENT_TABLE);

select * from CHILD_TABLE 其中 FOREIGNKEY 不在(从 PARENT_TABLE 中选择 PRIMARYKEY);

Delete these Orphan records, Commit the changes and restart the server if required.

删除这些孤立记录,提交更改并根据需要重新启动服务器。

This solved my exception. You may try the same.

这解决了我的异常。你也可以试试。

回答by ChiranjeeviIT

Main reason behind this issue is data mismatch, for example i have entity mapping class called "X" and it has column "column1" and it has reference to the table "Y" column "column1" as below

这个问题背后的主要原因是数据不匹配,例如我有一个名为“X”的实体映射类,它有“column1”列,它引用了表“Y”列“column1”,如下所示

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "column1", referencedColumnName = "column1")
public Y getColumn1() {
    return Y;
}

In this if X table column1 has value but Y table column1 is not having the value. Here link will be failed.

在这种情况下,如果 X 表 column1 有值但 Y 表 column1 没有值。这里链接将失败。

This is the reason we will get Hibernate objectNotFound exception

这就是我们会得到Hibernate objectNotFound 异常的原因

This issue can also be resolved by creating proper data model like creating proper indexing and constraints (primary key/foreign key) ..

这个问题也可以通过创建适当的数据模型来解决,比如创建适当的索引和约束(主键/外键)..

回答by Chris94

I have found that in Oracle this problem can also be caused by a permissions issue. The ProblemClass instance referred to by the MyDbObject instance may exist but have permissions that do not allow the current user to see it, even though the user can see the current MyDbObject.

我发现在 Oracle 中这个问题也可能是由权限问题引起的。MyDbObject 实例引用的 ProblemClass 实例可能存在,但具有不允许当前用户查看它的权限,即使用户可以查看当前 MyDbObject。