java - org.hibernate.ObjectNotFoundException : No row with the given identifier exists, 但它确实

标签 java sql hibernate jakarta-ee

我遇到了一个无法修复的 hibernate 问题。

设置:Java EE、网络应用、Hibernate 3.2、Tomcat 6、Struts 2。

基本上,我使用我的服务器逻辑(一个 struts 操作)持久化一个对象,然后尝试将该数据提取到下一页并显示它。

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

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

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [msc.model.Picture#73]

为了让事情变得更加困惑,当我重新启动 Tomcat 并尝试访问同一个对象时,我没有收到错误 - Hibernate 发现该行很好。

如果我执行一些其他操作,Hibernate 也将能够看到该行 - 可能在数据库中到处添加一行,甚至不在同一张表上。

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

这是我的 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>

这是我的两个映射文件:

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

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

如果您需要更多信息,请告诉我,我很乐意提供帮助。

(注意:这不是这个问题的重复,场景不一样"No row with the given identifier exists" although it DOES exist)

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

保存对象的代码

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

hib_ses.beginTransaction();

hib_ses.save(user);

hib_ses.getTransaction().commit(); 

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

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;
    }

最佳答案

发生这种情况是因为您插入了一些本应作为外键但未引用任何内容的内容。 检查您的数据库是否存在该键(即使它在其他表的数据库中)。

关于java - org.hibernate.ObjectNotFoundException : No row with the given identifier exists, 但它确实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984837/

相关文章:

java - setPixel() 返回 NullPointerException

java - 使用 Mybatis 检查 SQL 注入(inject)的输入

sql - 从远程计算机访问 SQL - WMI 命名空间

java - 违反 hibernate 和完整性约束

java - @ElementCollection计数频率

java - Hibernate createSQLQuery 不接受 Left 函数

Java - 如何在 Apache Ivy 中创建 JavaDocs

java - 如何定义具有多个值的数组?

sql - 我的查询的哪一部分是错误的?解除嵌套函数

mysql - 当我尝试在数据库中查找和替换 javascript 时,为什么会收到 SQL 错误