org.hibernate.ObjectNotFoundException: 不存在具有给定标识符的行

org.hibernate.ObjectNotFoundException: No row with the given identifier exists

我正在学习 javaEE。在我放置注释 OneToManyManyToOne 之前一切正常。但后来我看到这个错误:

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]

我无法 select 数据库或插入的所有信息。我使用模式 DAO,Hibernate

类别

    @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<Posts> post;
//then get and set

帖子

@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

接口 CategoryDao

public interface CategoryDao {
    public Collection getAllCategory();
}

接口 PostDao

public interface PostDao {
   public Collection getIndexPost();
}

CategoryDaoImpl

public Collection getAllCategory() {
        Session session = null;
        List categoris = new ArrayList<Category>();
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            categoris = session.createCriteria(Category.class).list();
        }catch(Exception e) {
            System.out.println("getAllCategory "+ e);
        }finally{
            if(session != null && session.isOpen())
                session.close();
        }
        return categoris;
    }

PostDaoImpl

public Collection getIndexPost() {
        List<Posts> 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;
    }

而这个 servlet 输出类别和 posts

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 && session.isOpen())
                    session.close();
            }

UPD:

现在可以正常输出信息,但我无法在 posts 列中添加 idCat 在我像这样添加 post 之前:

    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() && !namepost.isEmpty() && 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 && session.isOpen())
                        session.close();
                }

但现在我将帖子改成这样

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

我不知道如何添加 post 值 idCat。在 table post(id, namePost, Text, idCat) 中。类别(id,名称Cat) 我尝试了但没有成功

你的映射没有意义。一个类别有多个 post,但是唯一标识 post 的 post 的 ID(根据您的映射)也是它所属类别的 ID:

@ManyToOne(optional = false)
@JoinColumn(name = "ID", insertable=false, updatable=false)
private Category category;

您需要 Post 中的一些 CATEGORY_ID 列,该列应该是 ManyToOne 关联的 JoinColumn。