forum member
I need one help from you all. I am having two POJO model with one to many relationship. my project pojo is below
@Entity
@Table(name = "project")
public class Project implements java.io.Serializable {
private Integer projectid;
private Date enddate;
private String projectdesc;
private String projectname;
private String projecttitle;
private Date startdate;
private Set<Task> tasks = new HashSet<Task>(0);
public Project() {
}
public Project (Integer id,String projectname, String projecttitle, String projectdesc, Date startdate, Date enddate) {
this.projectid = id;
this.enddate = enddate;
this.projectdesc = projectdesc;
this.projectname = projectname;
this.projecttitle = projecttitle;
this.startdate = startdate;
}
public Project(Date enddate, String projectdesc, String projectname,
String projecttitle, Date startdate, Set<Task> tasks) {
this.enddate = enddate;
this.projectdesc = projectdesc;
this.projectname = projectname;
this.projecttitle = projecttitle;
this.startdate = startdate;
this.tasks = tasks;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "projectid", unique = true, nullable = false)
public Integer getProjectid() {
return projectid;
}
public void setProjectid(Integer projectid) {
this.projectid = projectid;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "enddate", length = 19)
public Date getEnddate() {
return enddate;
}
public void setEnddate(Date enddate) {
this.enddate = enddate;
}
@Column(name = "projectdesc")
public String getProjectdesc() {
return projectdesc;
}
public void setProjectdesc(String projectdesc) {
this.projectdesc = projectdesc;
}
@Column(name = "projectname")
public String getProjectname() {
return projectname;
}
public void setProjectname(String projectname) {
this.projectname = projectname;
}
@Column(name = "projecttitle")
public String getProjecttitle() {
return projecttitle;
}
public void setProjecttitle(String projecttitle) {
this.projecttitle = projecttitle;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "startdate", length = 19)
public Date getStartdate() {
return startdate;
}
public void setStartdate(Date startdate) {
this.startdate = startdate;
}
@OneToMany(cascade = CascadeType.ALL, fetch =FetchType.EAGER)
@JoinTable(name = "project_task", joinColumns = { @JoinColumn(name = "projectid") }, inverseJoinColumns = { @JoinColumn(name = "taskid") })
public Set<Task> getTasks() {
return tasks;
}
public void setTasks(Set<Task> tasks) {
this.tasks = tasks;
}
}
and my task pojo is
@Entity
@Table(name = "task")
public class Task implements java.io.Serializable {
private Integer taskid;
private Integer depth;
private Double duration;
private String durationunit;
private Date enddate;
private Integer parentid;
private Integer percentdone;
private Integer priority;
private Date startdate;
private Integer taskindex;
private String taskname;
public Task() {
}
public Task(Integer depth, Double duration, String durationunit,
Date enddate, Integer parentid, Integer percentdone,
Integer priority, Date startdate, Integer taskindex,
String taskname) {
this.depth = depth;
this.duration = duration;
this.durationunit = durationunit;
this.enddate = enddate;
this.parentid = parentid;
this.percentdone = percentdone;
this.priority = priority;
this.startdate = startdate;
this.taskindex = taskindex;
this.taskname = taskname;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "taskid", unique = true, nullable = false)
public Integer getTaskid() {
return taskid;
}
public void setTaskid(Integer taskid) {
this.taskid = taskid;
}
@Column(name = "depth")
public Integer getDepth() {
return depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
@Column(name = "duration", precision = 22, scale = 0)
public Double getDuration() {
return duration;
}
public void setDuration(Double duration) {
this.duration = duration;
}
@Column(name = "durationunit")
public String getDurationunit() {
return durationunit;
}
public void setDurationunit(String durationunit) {
this.durationunit = durationunit;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "enddate", length = 19)
public Date getEnddate() {
return enddate;
}
public void setEnddate(Date enddate) {
this.enddate = enddate;
}
@Column(name = "parentid")
public Integer getParentid() {
return parentid;
}
public void setParentid(Integer parentid) {
this.parentid = parentid;
}
@Column(name = "percentdone")
public Integer getPercentdone() {
return percentdone;
}
public void setPercentdone(Integer percentdone) {
this.percentdone = percentdone;
}
@Column(name = "priority")
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "startdate", length = 19)
public Date getStartdate() {
return startdate;
}
public void setStartdate(Date startdate) {
this.startdate = startdate;
}
@Column(name = "taskindex")
public Integer getTaskindex() {
return taskindex;
}
public void setTaskindex(Integer taskindex) {
this.taskindex = taskindex;
}
@Column(name = "taskname")
public String getTaskname() {
return taskname;
}
public void setTaskname(String taskname) {
this.taskname = taskname;
}
}
the project pojo is associated with task as one-to-many relationship. Now I want to delete one task based on the id I am passing, but the problem is that I am not able to delete the foreign key of the join table project_task.
I tried below code to delete the project_task first
Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
Query query = session.createQuery("delete from project_task where taskid="+id);
query.executeUpdate();
and then tried to delete the task with below code
Object record = hibernateTemplate.load(Task.class, id);
hibernateTemplate.delete(record);
but don't know why delete is not happening here. Any one having idea what I am doing wrong in my code which doesn't allow the delete to be happened
Try breaking the association first:
Task record = hibernateTemplate.load(Task.class, id);
Project project = hibernateTemplate.load(Project.class, projectId);
project.getTasks().remove(record);
hibernateTemplate.update(project);
hibernateTemplate.delete(record);
If you don't have the project id available, you might want to create an inverse ManyToOne relationship inside task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With