Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't delete child entity without deleting parent entity, regardless of CascadeTypes?

I'm trying to connect an entity (User) to entities they create which will be Surveys.

I have two repositories, one UserRepository and one SurveyRepository. I can load Surveys according to which User has them and currently they are all mapped by the User_ID, which is a field on the Survey entity.

However, when I try to remove a Survey, this removes my User whenever I define CascadeType.ALL.

But when I don't use that, I get another error "Caused by: java.sql.SQLIntegrityConstraintViolationException:"

I'm gussing this is all related to the password encryption I'm using, but I am not even trying to delete the User entity, I'm just deleting the Survey, which holds a reference, or an ID to the Survey..

I've tried CascadeType.All on both sides, and I've tried not having any CascadeType at all as well.. If I have it on both sides, this deletes the user whenever I tell my surveyRepository.delete(currentSurvey); And whenever I don't have it on both sides, I get the exception above..

User Entity:

    @Entity
@Table(name = "user")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "user_id")
  private Long id;

  @NotEmpty
  @Email
  @Column(unique = true)
  private String email;
  private String password;

  @NotBlank

  private String username;

  @NotBlank
  private String firstName;

  @NotBlank
  private String lastName;

  @NotBlank private String role;




 @OneToMany(fetch = FetchType.EAGER)
  private Set<Survey> surveys = new HashSet<>();

Survey Entity:

@Entity
@Table(name = "survey")

public class Survey {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "survey_id")
  private Long id;

  private String title, creator, description;
  private LocalDate date = LocalDate.now();

  @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "survey_id")

  @OrderBy("position ASC")
  private Set<Question> questions = new HashSet<>();

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "user_id")
  private User user;

I'm just not sure how I can tell JPA/Hibernate not to touch the User whenever we delete the Survey.

It doesn't matter if I save the User with Survvey or not does it?

Basically I've tried a lot of options and I figure I'm not quite grasping the issue, and I suspect it's about the annotations on the User side, but I still feel as if I should be able to delete the child entity with no problem at all since I am not touching the parent entity?

like image 993
harrigaturu Avatar asked Oct 16 '25 02:10

harrigaturu


1 Answers

This is because of EAGER fetch type in User class for surveys.
You delete survey but because it is existed on surveys set in user yet, it wouldn't be deleted actually.
You need to do like this:

// User class
 @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="user")
 private Set<Survey> surveys = new HashSet<>();
//Survey class
 @ManyToOne
 @JoinColumn(name = "user_id")
 private User user;
like image 143
Ebrahim Pasbani Avatar answered Oct 18 '25 21:10

Ebrahim Pasbani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!