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

时间:2015-03-29 09:26:27

标签: java hibernate servlets dao

我正在学习javaEE。在我添加注释OneToManyManyToOne之前,一切运作良好。但后来我看到了这个错误:

  

信息:HHH000327:执行加载命令时出错:   org.hibernate.ObjectNotFoundException:没有给定的行   标识符存在:[app.web.landingpage.object.Category#28]   getIndexPost org.hibernate.ObjectNotFoundException:没有行的   给定标识符存在:[app.web.landingpage.object.Category#28]

我无法选择db或insert上的所有信息。我使用模式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 输出类别和帖子

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:

现在输出信息很好,但我无法添加帖子 idCat 在我添加这样的帖子之前:

    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。在表格中(id,namePost,Text,idCat)。类别(id,nameCat) 我尝试但没有成功

1 个答案:

答案 0 :(得分:5)

你的映射毫无意义。一个类别有几个帖子,但帖子的ID(唯一标识帖子)是(根据你的映射)也是它所属类别的ID:

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

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