【问题标题】:how to delete the record from the join table in hibernate如何从休眠中的连接表中删除记录
【发布时间】:2012-02-23 09:31:10
【问题描述】:

论坛成员

我需要你们所有人的帮助。我有两个具有一对多关系的 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);

但不知道为什么这里没有发生删除。任何人都知道我在代码中做错了什么不允许删除发生

【问题讨论】:

  • 在任务端使用连接表而不是外键来处理一对多关系是否有特殊原因?
  • 我只希望我的数据库是干净的。我的 project_task 表将管理项目和任务之间的所有关系。一个项目 ID 与 project_task 表中的许多任务 ID 相关联。现在一切正常,当我想删除任务时我只是卡住了。你有什么解决方案?

标签: java hibernate spring


【解决方案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,您可能希望在任务内创建反向多对一关系。

【讨论】:

  • 我尝试了您的解决方案,但我的控制台给了我错误 ERROR org.hibernate.LazyInitializationException - 无法初始化代理 - 无会话 那么现在有什么问题??知道为什么会出现错误
  • 这很奇怪,因为任务集合是预先加载的。哪个语句抛出它?这可能是休眠模板的问题,据我所知,每个调用都是它自己的会话。你能切换到正常的休眠会话(例如没有休眠模板)吗?
  • 感谢您的回复。将休眠模板更改为正常休眠会话后,一切都开始正常工作。
【解决方案2】:

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

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

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

【讨论】:

  • 我实际上已经在我的项目 Pojo 中实现了 @OneToMany(cascade = CascadeType.ALL, fetch =FetchType.EAGER)。我只想从 project_task 表中删除我的任务及其所有关联的外键,您有任何解决方案吗?
  • 请告诉我们您遇到了什么错误...如果您没有任何错误,请检查您的数据库而不是代码:) hibernate 有一些缓存的东西..
  • Hibernate 没有给我错误 namde 无法删除或更新父行:外键约束失败 (primavera.project_task, CONSTRAINT FK3800AAEBACB7568D FOREIGN KEY (taskid ) REFERENCES task (taskid)) 由于连接表 project_task 中包含 taskid,因此删除过程无法删除该任务。那么如何删除包含taskid的连接表project_task
  • 如果你只是这样设置会发生什么:tasks.setTasks(---set without that task---); 不工作吗?
  • Alex 你误会了我想从创建的连接表 project_task 中删除 Task 对象。如果我根据 Id 删除任务,则会收到上述错误。那么有没有更好的方法可以成功删除任务,并且我的联接表 project_task 的 taskid 也被删除。 ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多