JPA hibernate - delete children from the list

Asked

Viewed 351 times

0

public class A implements Serializable {

   @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
   @JoinColumn(name = "A_ID")
   private List<B> bList;

}

public class B implements Serializable {
   @JoinColumn(name = "A_ID", referencedColumnName = "ID")
   @ManyToOne(optional = false)
   private A a;
}

With a lot of cost I managed to get Hibernate to leave the Foreign key of table A inside B as null in a register update, but with orphanRemoval = true it should delete these records from table B, which is not happening. How to solve this problem?

  • When you carry the Object A to be deleted, the variable List<B> is it filled? I wonder why the FetchType.LAZY causes the list to be loaded only if it is accessed.

  • Airton, can you pass which error you generate? Or if you are able to delete item A and item B is orphaned?

  • The LSITA is loaded straight including Hibernate until it leaves the foregin Keys of table B as null, but it does not delete these lines.... it simply leaves nullas and does not erase them

  • When using the Eclipse link only needed by @Privateowned q he already did all this... however agr in Hibernate I’m catching a little

  • Try to map only one side with @JoinColumn (the side that has the foreign key) and the other use the mappedBy. The way it is there, there’s no dominant side to the relationship and the Hibernant may be getting lost.

  • previously table A had mappedby on @Onetomany, and table B had joinColumn, but this way it was not even setting null to Foreign key of table B when saving the object of table A

  • I realized q if I remove the Cascadetype.All and put only the Cascadetype.Persist and the Cascadetype.Remove it starts to remove correctly. However I still need Cascadetype.Merge and that’s where the problem arises. If I put Cascadetype.Merge, the removal of the items stops working

  • I set up the same structure in a desktop project only with Hibernate and my current project uses springboot. In desktop design works perfectly, in problem springboot project

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.