Hie all, I'm quite new with Hibernate. I'm currently using Hibernate 3.4 and having a problem to perform deletion operation to a persistance object in my project implementing SPRING, Hibernate & Tiles togather.
I have an object BlogEntry that has 2-manytoone relationship with object Company and BlogEntryStatus as below:
Code:
package KSA.object;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="BlogEntry")
public class BlogEntry implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long blogEntryId;
private String title;
private String entryBody;
private User postedBy;
private Date postedDate;
private Company company;
private BlogEntryStatus blogEntryStatus;
@Id
@GeneratedValue
@Column(name="blogEntryId")
public long getBlogEntryId() {
return blogEntryId;
}
public void setBlogEntryId(long blogEntryId) {
this.blogEntryId = blogEntryId;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="entryBody")
public String getEntryBody() {
return entryBody;
}
public void setEntryBody(String entryBody) {
this.entryBody = entryBody;
}
@Column(name="postedDate")
public Date getPostedDate() {
return postedDate;
}
public void setPostedDate(Date postedDate) {
this.postedDate = postedDate;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
public User getPostedBy() {
return postedBy;
}
public void setPostedBy(User postedBy) {
this.postedBy = postedBy;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "companyId")
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "statusId")
public BlogEntryStatus getBlogEntryStatus() {
return blogEntryStatus;
}
public void setBlogEntryStatus(BlogEntryStatus blogEntryStatus) {
this.blogEntryStatus = blogEntryStatus;
}
}
When I click delete, the controller passed the instruction to the DAO to execute deletion operation of object BlogEntry through its Id as below:
Controller's:
Code:
@RequestMapping("/blog/deleteBlogEntry")
public ModelAndView deleteBlogEntry(HttpServletRequest request)
{
BlogEntry blogEntry = blogEntryDAO.getBlogEntry(Long.parseLong(request.getParameter("blogEntryId")));
blogEntryDAO.deleteBlogEntry(blogEntry);
return new ModelAndView("redirect:../landingPage.html");
}
DAO implementation:
Code:
@Override
public boolean deleteBlogEntry(BlogEntry blogEntry) {
// TODO Auto-generated method stub
boolean blogEntryDeleted = false;
ht.delete(blogEntry);
ht.flush();
blogEntryDeleted = true;
return blogEntryDeleted;
}
The operation however delete other data on other tables that are not related to BlogEntry object. Where my mistake is??? Can somebody show me the correct method to do deletion operation in Hibernate???
Thanks