Reputation: 9
I am using hibernate version 4.3.11.Final. When I try to delete a product from the jsp page, it enters the controller and executes it completely. However, it shows this message on the console : org.hibernate.event.internal.DefaultDeleteEventListener deleteTransientEntity INFO: HHH000114: Handling transient entity in delete processing
Does anyone have a solution?
This is my controller:
@RequestMapping("/deleteProduct")
public ModelAndView delete(@ModelAttribute("product") Product product, BindingResult result)
{
System.out.println("In delete");
productDAO.delete(product);
System.out.println("Product Successfully deleted");
return new ModelAndView("AdminProduct");
}
This is my JSP:
<c:forEach items="${products}" var="product">
<tr
style="background-color: white; color: black; text-align: center;"
height="30px">
<td><c:out value="${product.p_id}" /></td>
<td><c:out value="${product.p_name}" /></td>
<td><c:out value="${product.p_desc}" /></td>
<td><c:out value="${product.price}" /></td>
<td><form:form action="deleteProduct" modelAttribute="product">
<input type="submit" value="Delete" />
</form:form></td>
<td><form:form action="editProduct" modelAttribute="product">
<input type="submit" value="Edit" />
</form:form></td>
</tr>
</c:forEach>
And, this is my DAOImpl class for this Entity:
package com.niit.shoppingcart.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.niit.shoppingcart.model.Product;
@Repository(value = "productDAO")
public class ProductDAOImpl implements ProductDAO {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public boolean save(Product product) {
try {
sessionFactory.getCurrentSession().save(product);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@Transactional
public boolean update(Product product) {
try {
sessionFactory.getCurrentSession().update(product);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@Transactional
public boolean delete(Product product) {
try {
sessionFactory.getCurrentSession().delete(product);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@Transactional
public Product get(String id) {
String hql = "from Product where id = " + " ' " + id + " ' ";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<Product> plist = (List<Product>) query.list();
if (plist == null || plist.isEmpty()) {
return null;
} else {
return plist.get(0);
}
}
@SuppressWarnings("unchecked")
@Transactional
public List<Product> listAll() {
String hql = "from Product";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
}
}
This is the Entity class:
package com.niit.shoppingcart.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "PRODUCT")
@Component
public class Product {
@Id
private String p_id;
private String p_name;
private String p_desc;
private int price;
public String getP_id() {
return p_id;
}
public void setP_id(String p_id) {
this.p_id = p_id;
}
public String getP_name() {
return p_name;
}
public void setP_name(String p_name) {
this.p_name = p_name;
}
public String getP_desc() {
return p_desc;
}
public void setP_desc(String p_desc) {
this.p_desc = p_desc;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
Upvotes: 1
Views: 1547
Reputation: 1235
Try to use transactions with sessions. Maybe your changes are not commited with the session.
Transaction transaction = sessionFactory.getCurrentSession().beginTransaction();
// your changes
transaction.commit(); // or you can rollback on exceptions
Or try to change your @Transactional
annotation to @org.springframework.transaction.annotation.Transactional
And why do you need @Component
on your entity class?
Anyway I think it's a problem with transactions and transaction manager
Upvotes: 0
Reputation: 338
In the controller, you get a Product Object product, and the product is not under the control of Hibernate. So if you want to delete the record with the product entity, you should select it from the db before your deletion. Like below,
Product p = productDAO.get(product.getId());
productDAO.delete(p);
and a better way to delete the record is that adding a method in your ProductDAO like deleteById(String id)
and call it with the parameter product.getId()
;
Upvotes: 2