如何从hibernate中的连接表中删除记录

时间:2012-02-23 09:31:10

标签: java hibernate spring

论坛会员

我需要你们所有人的帮助。我有两个POJO模型,有一对多的关系。 我的项目pojo位于

之下
@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;
    }


}

,我的任务pojo

@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;
    }


}

项目pojo与任务相关联为一对多关系。现在我想根据我传递的id删除一个任务,  但问题是我无法删除连接表project_task的外键。

我尝试下面的代码首先删除project_task

Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from project_task where taskid="+id);
        query.executeUpdate();

然后尝试使用以下代码删除任务

Object record = hibernateTemplate.load(Task.class, id);
hibernateTemplate.delete(record);

但不知道为什么删除不会发生在这里。任何人都知道我的代码中的错误哪个不允许删除

2 个答案:

答案 0 :(得分:1)

首先尝试破解关联:

Task record = hibernateTemplate.load(Task.class, id);
Project project = hibernateTemplate.load(Project.class, projectId);
project.getTasks().remove(record);
hibernateTemplate.update(project);
hibernateTemplate.delete(record);

如果您没有可用的项目ID,则可能需要在任务中创建反向ManyToOne关系。

答案 1 :(得分:0)

也许您应该更改Project中的任务集,然后尝试删除任务...

我认为你也可以使用cascade.type All(在setTasks上)......

您必须从任务集中删除您要删除的任务...并更新实体