There was a problem:

When a child is deleted, the parent is deleted. How to fix it.

The Worker has a List of Positions (A Position is a separate entity). How to delete a post from a Worker when deleting a post.

All heters and setters and designers removed. I use Spring Boot + Spring Data + Hibernate

This is Worker

@Entity @Table(name = "worker") @Inheritance(strategy = InheritanceType.JOINED) public class Worker implements Serializable { @Id @GeneratedValue @Column(name = "id") private Long id; @Column(name = "firstName", length = 256) private String firstName; @Column(name = "lastName", length = 256) private String lastName; @Column(name = "email") private String email; @Column(name = "phoneNumber") private Long phoneNumber; @Column(name = "shiftSalary", nullable = true) private Long shiftSalary; @ManyToMany(cascade = { CascadeType.REMOVE }, mappedBy="workerList") private List<Position> allPosition; @Column(name = "countShift", nullable = true) private Long countShift; @Column(name = "salary", nullable = true) private Long salary; 

This Position

 @Entity @Table(name = "position") public class Position { @Id @GeneratedValue @Column(name = "id") private Long id; @Column(name = "jobName") private String jobName; @ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }) @JoinTable(name = "permissions_allPosition", joinColumns = {@JoinColumn(name = "position_id")}, inverseJoinColumns = {@JoinColumn(name = "worker_id")}) private List<Worker> workerList; 
  • Hey. If the answer helped, do not forget to vote for him ... - Cepr0

2 answers 2

It is necessary to make an entity relation differently:

 @Entity public class Worker { //... @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) private final Set<Position> positions = new HashSet<>(); @PrePersist public void addPositions() { positions.forEach(position -> position.getWorkers().add(this)); } @PreRemove public void removePositions() { positions.forEach(position -> position.getWorkers().remove(this)); } //... } @Entity public class Position { //... @ManyToMany(mappedBy = "position") private final Set<Worker> workers = new HashSet<>(); public Set<Worker> getWorkers() { return workers; } //... } 

This is a bidirectional ManyToMany , so the Worker needs more helper methods for adding and deleting Positions.

Unidirectional helper methods are not needed.

See the manual


I would not use bidirectional - because of it, the code becomes more complicated, and there are no advantages, like bidirectional OneToMany , (the third table of connections of the type worker_position will still be involved).

And if you need to get a list of Workers for a specific Position, it is easy to do this in the following way:

 public interface WorkerRepository extend JpaRepository<Worker, Long> { List<Worker> findByPositions(Position position); } 

Working example with tests .

  • Thanks, it helped! But really it can not be done without third-party methods? Specifically, with such a connection that I had, when you delete a position, it is automatically removed from the sheet of the worker, but when you remove the worker, all positions that are in this sheet are deleted ( I originally had a question in question) - Zaytsev_Dmitry
  • @Zaytsev_Dmitry is not allowed. it is not a telepath, does not know what your methods. Well, as I wrote, it is better to use unidirectional here - ext. methods are not needed. - Cepr0

Try:

 @PreRemove private void removePositionsFromWorkers() { for (Worker w : workerList) { w.getAllPosition().remove(this); } } 

Of course, there must be a corresponding getter. For this to work, positions must contain an up-to-date list of workers. Ie every time when you add a position to the sheet of the positions of the worker, you need to add the workers to the sheet of the workers of the position (feedback). Will not work with CascadeType.ALL