如何从休眠中的联接表中删除记录 [英] how to delete the record from the join table in hibernate

查看:50
本文介绍了如何从休眠中的联接表中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

论坛成员

我需要你们所有人的帮助.我有两个具有一对多关系的POJO模型.我的项目pojo在下面

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


}

我的任务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的外键.

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.

我尝试下面的代码首先删除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

推荐答案

尝试首先断开关联:

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关系.

If you don't have the project id available, you might want to create an inverse ManyToOne relationship inside task.

这篇关于如何从休眠中的联接表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆