Java org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行
我在学javaEE。在我添加注释之前,Java org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行,java,hibernate,servlets,dao,Java,Hibernate,Servlets,Dao,我在学javaEE。在我添加注释之前,OneToMany和manytone是否一切正常。但我看到了这个错误: 信息:HH000327:执行加载命令时出错: org.hibernate.ObjectNotFoundException:没有具有给定 标识符存在:[app.web.landingpage.object.Category#28] getIndexPost org.hibernate.ObjectNotFoundException:没有包含 给定标识符存在:[app.web.landing
OneToMany
和manytone
是否一切正常。但我看到了这个错误:
信息:HH000327:执行加载命令时出错:
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
接口类别DAO
public interface CategoryDao {
public Collection getAllCategory();
}
public interface PostDao {
public Collection getIndexPost();
}
接口PostDao
public interface CategoryDao {
public Collection getAllCategory();
}
public interface PostDao {
public Collection getIndexPost();
}
类别daoimpl
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;
}
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;
}
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;
我不知道如何添加后值idCat。在表post中(id、namePost、Text、idCat)。类别(id、nameCat)
我尝试了,但没有成功您的映射毫无意义。一个类别有多篇文章,但唯一标识该文章的文章ID(根据您的映射)也是它所属类别的ID:
@ManyToOne(optional = false)
@JoinColumn(name = "ID", insertable=false, updatable=false)
private Category category;
在Post中需要一些CATEGORY_ID列,该列应该是多通协会的JoinColumn。不要更新此已回答且已接受的问题。问另一个单独的问题。