Issue
I'm learning javaEE. Before I put the annotation OneToMany
and ManyToOne
is everything working well. But then I see this error:
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]
I can't select all information on db or insert. I use pattern DAO, Hibernate
Category
@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
Posts
@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
interface CategoryDao
public interface CategoryDao {
public Collection getAllCategory();
}
interface 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;
}
And this servlet outputs category and 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:
now output information fine, but i cant add in posts column idCat before i add posts like this:
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();
}
but now i change Posts like this
@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;
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
Solution
Your mapping makes no sense. A category has several posts, but the post's ID, which uniquely identifies the post, is (according to your mapping) also the ID of the category it belongs to:
@ManyToOne(optional = false)
@JoinColumn(name = "ID", insertable=false, updatable=false)
private Category category;
You need some CATEGORY_ID column in Post, and that column should be the JoinColumn of the ManyToOne association.
Answered By - JB Nizet
Answer Checked By - Gilberto Lyons (JavaFixing Admin)