org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行,但它确实存在

nas*_*sty 26 java sql hibernate java-ee

我有一个无法修复的休眠问题.

设置:Java EE,Web应用程序,Hibernate 3.2,Tomcat 6,Struts 2.

基本上,我使用我的服务器逻辑(一个struts动作)来持久化对象,然后尝试将该数据拉出来用于下一页并显示它.

我保存对象后检查数据库,果然,我可以在那里看到包含所有数据的行.

但是当我尝试检索它时,我得到了这个:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [msc.model.Picture#73]
Run Code Online (Sandbox Code Playgroud)

为了使事情变得更加混乱,当我重新启动Tomcat并尝试访问同一个对象时,我没有得到错误 - Hibernate发现行就好了.

如果我做一些其他操作,Hibernate也能看到该行 - 可能在这里和那里添加一行到数据库,甚至不在同一个表上.

从这一切我怀疑一个Hibernate错误,但我是一个Hibernate新手所以我可能错了.请帮忙!我用Google搜索并用谷歌搜索无济于事.

这是我的Hibernate配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/msc</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">-------</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">80</property>
        <property name="current_session_context_class">thread</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <mapping resource="msc/model/Picture.hbm.xml"/>
        <mapping resource="msc/model/Comment.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

这是我的两个映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="msc.model.Picture" table="PICTURE">
    <id column="PICTURE_ID" name="id">
      <generator class="native"/>
    </id>
    <property name="story"/>
    <property name="email"/>
    <property name="category"/>
    <property name="state"/>
    <property name="ratings"/>
    <property name="views"/>
    <property name="timestamp"/>
    <property name="title"/>
    <property lazy="true" name="image" type="blob">
      <column name="IMAGE"/>
    </property>
    <property lazy="true" name="refinedImage" type="blob">
      <column name="REFINEDIMAGE"/>
    </property>
    <property lazy="true" name="thumbnail" type="blob">
      <column name="THUMBNAIL"/>
    </property>
    <bag cascade="save-update" lazy="true" name="comments" table="COMMENT">
      <key column="PICTURE"/>
      <one-to-many class="msc.model.Comment"/>
    </bag>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="msc.model.User" table="USER">
    <id column="USER_ID" name="id">
      <generator class="native"/>
    </id>
    <property name="username"/>
    <property name="email"/>
    <bag cascade="save-update" lazy="true" name="pictures" table="PICTURE">
      <key column="USER"/>
      <one-to-many class="msc.model.Picture"/>
    </bag>
    <bag cascade="save-update" lazy="true" name="comments" table="COMMENT">
      <key column="USER"/>
      <one-to-many class="msc.model.Comment"/>
    </bag>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请告诉我,我很高兴.

(注意:这不是这个问题的重复,场景是不一样的"没有给定标识符的行存在",尽管它存在)

编辑:根据要求,发布Java代码:

用于保存对象的代码

Session hib_ses = HibernateUtil.getSessionFactory().getCurrentSession();

hib_ses.beginTransaction();

hib_ses.save(user);

hib_ses.getTransaction().commit(); 
Run Code Online (Sandbox Code Playgroud)

显示数据的代码(本例中为图像)

public class ImageAction extends ActionSupport implements ServletResponseAware, SessionAware {

    private HttpServletResponse response;
    Map session;
    private Long id;
    private int thumbnail;
    private InputStream inputStream;

    @Override
    public String execute() throws Exception {
        response.setContentType("image/jpeg");
        Session hib_session = HibernateUtil.getSessionFactory().getCurrentSession();
        hib_session.beginTransaction();

        //what is the ID now?
        Picture pic = (Picture) hib_session.load(Picture.class, getId());

        if (thumbnail == 1) {
            inputStream = (ByteArrayInputStream) pic.getThumbnail().getBinaryStream();
        } else {
            inputStream = (ByteArrayInputStream) pic.getRefinedImage().getBinaryStream();
        }

        hib_session.close();
        return SUCCESS;
    }
Run Code Online (Sandbox Code Playgroud)

Est*_*mps 16

检查所有映射和数据库设置.当您的数据库显示nullable ="true"时,您可能在外键关系中设置了一些not-null ="true".第一个引起INNER JOIN,第二个引起LEFT JOIN.

将日志级别设置为TRACE以查看所有步骤,并在检索对象时查找生成的SQL.


Ami*_*mar 16

发生这种情况是因为您插入了一些外键,但没有引用任何内容.检查数据库是否存在该密钥(即使它在其他表的数据库中).

  • 我确认,至少在我的情况下,这就是问题所在. (3认同)
  • 这是非常重要的检查,我的数据库有这个问题,如果我们没有意识到这个问题就会使我们陷入困境 (2认同)

Har*_*jar 5

只需检查您的数据库是否在您的特定表中提供了ID 73


Rak*_*esh 5

在多对一关系中,如果未找到映射的行,则需要告诉Hibernate需要做什么。您可以通过以下方式配置它:

Annotation:
@NotFound(action = NotFoundAction.IGNORE)
Mapping XML:
<many-to-one name="user" column="user_id" class="com.xyz.User" cascade="save-update, evict" not-found="ignore"/>
Run Code Online (Sandbox Code Playgroud)

  • `@NonFound(action = NotFoundAction.IGNORE)` 会将 `FetchType` 从 `LAZY` 切换为 `EAGER` (2认同)

TC1*_*TC1 0

我没有看到任何与代码中的异常相关的实际问题,您可能想尝试:

  • 检查事务是否刷新或者flush()提交后手动调用;
  • 通过调试器检查ID是否传递给load()它以及是否传递了正确的ID
  • 启用 Hibernate 打印生成的 SQL 和/或启用日志记录