Note that there are some explanatory texts on larger screens.

plurals
  1. POcan not delete the entity which have collection as child with hibernate
    primarykey
    data
    text
    <p>NOTE:</p> <p>This is a croass post at:<a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=1010241&amp;p=2443214#p2443214" rel="nofollow">hibernate forum</a></p> <p>Since I got no answer ,so I ask here to see if I can get some help. :)</p> <p>If this is solved here,I will post the answer in the hibernate forum. :)</p> <hr> <p>In my application,I have some entities which own some collections,when I have no idea to delete them.</p> <p>This is the core codes of my eneity:</p> <p>Code:</p> <pre><code>@Entity @Table( name = "t_task") public class Task { private int id; private List&lt;TaskStep&gt; steps = new ArrayList&lt;TaskStep&gt;(); public Task() { this.createTime = new Date(); } public Task(String name) { this(); this.name = name; } @Id @GeneratedValue public int getId() { return id; } @OneToMany( cascade = CascadeType.ALL) @JoinColumn( name = "task_id", nullable = false) @LazyCollection(LazyCollectionOption.FALSE) @IndexColumn( name = "position") public List&lt;TaskStep&gt; getSteps() { return steps; } // domain method public void addSteps(TaskStep ts) { steps.add(ts); ts.setTask(this); } public void removeStep(TaskStep ts) { steps.remove(ts); } // setter public void setId(int id) { this.id = id; } public void setSteps(List&lt;TaskStep&gt; steps) { this.steps = steps; for (TaskStep st : steps) { st.setTask(this); } } } //TaskStep: @Entity @Table( name = "t_taskstep") public class TaskStep { private int id; private List&lt;Operator&gt; operator = new ArrayList&lt;Operator&gt;(); private Task task; public TaskStep() {} @Id @GeneratedValue public int getId() { return id; } @ManyToMany( cascade = CascadeType.ALL) @LazyCollection(LazyCollectionOption.FALSE) public List&lt;Operator&gt; getOperator() { return operator; } @ManyToOne @JoinColumn( name = "task_id", nullable = false, updatable = false, insertable = false) public Task getTask() { return task; } // domain method start public void addOperator(Operator op) { operator.add(op); } // setter public void setId(int id) { this.id = id; } public void setOperator(List&lt;Operator&gt; operator) { this.operator = operator; } public void setTask(Task task) { this.task = task; } } //Operator: @Entity @Table( name = "t_operator") public class Operator { private int id; private List&lt;TaskStep&gt; steps = new ArrayList&lt;TaskStep&gt;(); public Operator() {} @Id @GeneratedValue public int getId() { return id; } // //setter public void setId(int id) { this.id = id; } public void setSteps(List&lt;TaskStep&gt; steps) { this.steps = steps; } @ManyToMany( mappedBy = "operator") public List&lt;TaskStep&gt; getSteps() { return steps; } } </code></pre> <p>In the db,there are tables of "t_task","t_operator","t_step","t_step_operator".</p> <p>I use this manner to remove the task: Code: taskDao.delTaskById(5);</p> <p>This is the dao:</p> <p>Code:</p> <pre><code> public void delTaskById(int id) { Task t = queryTaskById(id); Session sess = factory.getCurrentSession(); try { sess.beginTransaction(); sess.delete(t); // 1) sess.flush(); sess.getTransaction().commit(); } catch (HibernateException e) { sess.getTransaction().rollback(); } } </code></pre> <p>I got a error which said "can not delete or update a parent row....".</p> <p>Then I tried use the repalce the</p> <pre><code>sess.delete(t) </code></pre> <p>to </p> <pre><code>sess.createQuery("delete from Task t where t.id="+id).executeUpdate() </code></pre> <p>I got now error,but the task is not removed actually.</p> <p>I have set the cascade in the mapping. for example,for the taskstesps in the task object,I set cascade.type=all,so I think when hibernate try to delete the task,it should del its realted tasksteps as well,and when it try to delete a taskstep object, it will find that the "t_step_t_operator" table is referring the id in "t_step",so I also set "cascade=all" for the "step" field in task class. But seems that what is going on is not what I thought....</p> <p>What is the problem? I am going to crazy!</p> <p>Any one can do me a favor?</p> <p>BWT,what does the cascade meaning?</p> <p>For example: In the TaskStep calss,I have a List of Operators;</p> <pre><code> //Class:TaskStep. @ManyToMany( cascade = CascadeType.ALL) @LazyCollection(LazyCollectionOption.FALSE) public List&lt;Operator&gt; getOperator() { return operator; } </code></pre> <p>In the above exmaple,I set cascade = all,does this mean no matter what operation(curd) to the TaskStep,it will do the same operation to the Opeartors?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload