title: org.hibernate.ObjectNotFoundException: No row with the given identifier exists
body: <p>I'm learning javaEE. Before I put the annotation <code>OneToMany</code> and <code>ManyToOne</code> is everything working well. But then I see this error: </p>
  
  <blockquote>
    <p>INFO: HHH000327: Error performing load command :
    org.hibernate.ObjectNotFoundException: No row with the given
    identifier exists: [app.web.landingpage.object.Category#28]
    getIndexPost org.hibernate.ObjectNotFoundException: No row with the
    given identifier exists: [app.web.landingpage.object.Category#28]</p>
  </blockquote>
  
  <p>I can't select all information on db or insert. I use pattern DAO, Hibernate</p>
  
  <p><strong>Category</strong></p>
  
  <pre><code>    @Entity
      @Table(name="CATEGORY")
      public class Category {
  
          @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
          @Column(name = "ID", insertable=false, nullable=false)
          private int id;
  
          @Column(name="NAMECAT") 
          private String nameCat;
  
          @Column(name="INDEX") 
          private boolean index;
  
          @OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
          private Set&lt;Posts&gt; post;
  //then get and set
  </code></pre>
  
  <p><strong>Posts</strong></p>
  
  <pre><code>@Entity(name="Posts")
  @Table(name="POSTS")
  public class Posts implements java.io.Serializable{
      private static final long serialVersionUID = 1L;
  
      @Id @GeneratedValue
      @Column(name = "ID", unique = true, nullable = false)
      private int id;
  
      @Column(name="NAMEPOST", nullable = false) 
      private String namePost;
  
      @Column(name="TEXT", nullable = false) 
      private String text;
  
      @Column(name="IDCAT", nullable = false, insertable=false, updatable=false) 
      private int idCat;
  
      @ManyToOne(optional = false)
      @JoinColumn(name = "ID", insertable=false, updatable=false)
      private Category category;
  //then get and set
  </code></pre>
  
  <p><em>interface</em> <strong>CategoryDao</strong></p>
  
  <pre><code>public interface CategoryDao {
      public Collection getAllCategory();
  }
  </code></pre>
  
  <p><em>interface</em> <strong>PostDao</strong></p>
  
  <pre><code>public interface PostDao {
     public Collection getIndexPost();
  }
  </code></pre>
  
  <p><strong>CategoryDaoImpl</strong></p>
  
  <pre><code>public Collection getAllCategory() {
          Session session = null;
          List categoris = new ArrayList&lt;Category&gt;();
          try{
              session = HibernateUtil.getSessionFactory().openSession();
              categoris = session.createCriteria(Category.class).list();
          }catch(Exception e) {
              System.out.println("getAllCategory "+ e);
          }finally{
              if(session != null &amp;&amp; session.isOpen())
                  session.close();
          }
          return categoris;
      }
  </code></pre>
  
  <p><strong>PostDaoImpl</strong></p>
  
  <pre><code>public Collection getIndexPost() {
          List&lt;Posts&gt; posts = null;
          Session session = null;
          try{
              session = HibernateUtil.getSessionFactory().openSession();
              SQLQuery q = (SQLQuery) 
                      session.createSQLQuery("select p.* from posts p join category c on c.index=1");
              q.addEntity(Posts.class);
              posts = q.list();
          } catch(Exception e) { outputError("getIndexPost", e);
          } finally{ closeSession(session); }
          return posts;
      }
  </code></pre>
  
  <p>And this <strong>servlet</strong> outputs category and posts</p>
  
  <pre><code>Session session = HibernateUtil.getSessionFactory().getCurrentSession();
              session.beginTransaction();
  
              try{
  //get all category
                  Collection allcategory = Factory.getInstance().getCatDAO().getAllCategory();
  //get all post
                  Collection allpost = Factory.getInstance().getPostDAO().getIndexPost();
  
                  request.setAttribute("allcategory", allcategory);
                  request.setAttribute("allpost", allpost);
  
                  request.getRequestDispatcher("/index.jsp").forward(request, response); 
              } catch(Exception e) { 
                  System.out.println(e);
              } finally{
                  if(session!=null &amp;&amp; session.isOpen())
                      session.close();
              }
  </code></pre>
  
  <hr>
  
  <p><strong>UPD:</strong></p>
  
  <p>now output information fine, but i cant add in <strong>posts</strong> column <strong>idCat</strong>
  before i add posts like this:</p>
  
  <pre><code>    String[] catarrayid = request.getParameterValues("eachcat");//get cat what user use
              String textpost = request.getParameter("textpost"); //text post
              String namepost = request.getParameter("namepost"); //name post
  
              if(!textpost.isEmpty() &amp;&amp; !namepost.isEmpty() &amp;&amp; catarrayid!=null) {
  //open session              
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                  session.beginTransaction();
  
                  Posts post = new Posts();
  //this value take id category               
  int id = 0;
                  for(String s : catarrayid) {
                      id = Integer.parseInt(s);
                  }
  
  //create post object 
                  post.setnamePost(namepost);
                  post.setText(textpost);
  //this i add in table post value idCat              
  post.setIdCat(id);
  
                  try{
  //and i add
                      Factory.getInstance().getPostDAO().addPost(post);
                      response.sendRedirect("indexadmin");
                  }catch(Exception e) {
                      System.out.println(e);
                  }finally{
                      if(session!=null &amp;&amp; session.isOpen())
                          session.close();
                  }
  </code></pre>
  
  <p>but now i change <strong>Posts</strong> like this</p>
  
  <pre><code>@Id @GeneratedValue
      @Column(name = "ID", unique = true, nullable = false)
      private int id;
  
      @Column(name="NAMEPOST", nullable = false) 
      private String namePost;
  
      @Column(name="TEXT", nullable = false) 
      private String text;
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name = "IDCAT", insertable=false, updatable=false)
      private Category category;
  </code></pre>
  
  <p>and i dont know how add in post value idCat. In table post(id, namePost, Text, idCat). Category(id, nameCat)
  I try but not success</p>