org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行,但它可以 [英] org.hibernate.ObjectNotFoundException: No row with the given identifier exists, but it DOES

查看:333
本文介绍了org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行,但它可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



设置:Java EE,Web应用程序,Hibernate 3.2,Tomcat 6,Struts 2。基本上,我坚持一个对象与我的服务器逻辑(一个struts动作),然后试着将这些数据提取出来,然后显示出来。



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



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

  org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:为了让事情变得更加混乱,当我重新启动Tomcat并尝试使用这些代码时,并访问相同的对象,我没有得到错误 -  Hibernate发现该行很好。



如果我做了其他的事,Hibernate也可以看到该行操作 - 可能会在数据库中添加一行,甚至不会在同一张表上。



从这一切我都怀疑Hibernate的bug,但我是一个Hibernate的新手,所以我可能是错的。请帮忙!



以下是我的Hibernate配置:

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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>根< / property>
< property name =hibernate.connection.password> -------< / property>
<! - JDBC连接池(使用内置) - >
< property name =connection.pool_size> 80< / property>
< property name =current_session_context_class>线程< / property>
<! - 将所有执行的SQL回复到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.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =msc.model.Picturetable =PICTURE>
< id column =PICTURE_IDname =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 =truename =imagetype =blob>
< column name =IMAGE/>
< / property>
< property lazy =truename =refinedImagetype =blob>
< column name =REFINEDIMAGE/>
< / property>
< property lazy =truename =thumbnailtype =blob>
< column name =THUMBNAIL/>
< / property>
< bag cascade =save-updatelazy =truename =commentstable =COMMENT>
< key column =PICTURE/>
<一对多等级=msc.model.Comment/>
< / bag>
< / class>
< / hibernate-mapping>

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =msc.model.Usertable =USER>
< id column =USER_IDname =id>
< generator class =native/>
< / id>
< property name =username/>
< property name =email/>
< bag cascade =save-updatelazy =truename =picturestable =PICTURE>
< key column =USER/>
< / bag>
< bag cascade =save-updatelazy =truename =commentstable =COMMENT>
< key column =USER/>
<一对多等级=msc.model.Comment/>
< / bag>
< / class>
< / hibernate-mapping>

请让我知道,如果您需要更多信息,我很乐意遵守。



(注意:这不是这个问题的重复,情况不一样



存在于给定标识符存在(尽管它存在)>>不存在具有给定标识符的行

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

保存对象的代码

 会话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;
地图会话;
私人长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();

//现在ID是什么?
图片图片=(图片)hib_session.load(Picture.class,getId());

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

hib_session.close();
返回SUCCESS;


解决方案

检查所有映射和数据库设置。当你的数据库表示nullable =true时,你可能会在外键关系中设置一些not-null =true。第一个导致INNER JOIN,第二个导致LEFT JOIN。



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


I've got a hibernate problem that I can't fix.

The setup: Java EE, web app, Hibernate 3.2, Tomcat 6, Struts 2.

Basically, I persist an object with my server logic (a struts action), then try and pull that data out for the next page and display it.

I check the database after I save the object, and sure enough, I can see the row there with all the data.

But when I try and retrieve it I get this:

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

To make things even muddier, when I restart Tomcat and try and access the same object, I don't get the error - Hibernate finds the row just fine.

Hibernate will also be able to see the row if I do some other operations - maybe add a row here and there to the database, not even on the same table.

From all this I suspect a Hibernate bug, but I'm a Hibernate newbie so I am probably wrong. Please help! I've googled and googled to no avail.

Here is my Hibernate config:

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

Here are my two mapping files:

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

and

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

Please let me know if you need more info, I'm happy to oblige.

(note: this is not a duplicate of this question, the scenario is not the same "No row with the given identifier exists" although it DOES exist)

EDIT: as requested, posting Java code:

Code to save object

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

hib_ses.beginTransaction();

hib_ses.save(user);

hib_ses.getTransaction().commit(); 

Code to display data (an image in this case)

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

解决方案

Check all your mappings and database settings. It may be possible you are setting some not-null="true" in foreign key relations when your database says nullable="true". The first causes an INNER JOIN and the second causes LEFT JOIN.

Set log level to TRACE to see all steps and look for generated SQL when retrieving the objects.

这篇关于org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行,但它可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆