简体   繁体   中英

Cannot delete or update a parent row: a foreign key constraint fails when deleting entity with reference

I want to delete an instance of "UserRegistration" which has a reference to an instance of "Authority". When deleting the instance of "UserRegistration" via the repository I get following Error: "Cannot delete or update a parent row: a foreign key constraint fails ( seprojekt . authorities , CONSTRAINT authorities_ibfk_1 FOREIGN KEY ( userid ) REFERENCES users ( id ))" Is there a possibility of deleting the referenced Entity when the Instance of "UserRegistration" ist deleted? I tried this with Cascadetype and orphanRemoval in "UserRegistration" but nothing happened.

The Method which tries to delete the Entity:

@DeleteMapping
public boolean deleteUser(@RequestParam String username) {
    Optional<UserRegistration> optUserRegistration = uRepo.findByUsername(username);
    if(optUserRegistration.isPresent()) {
        System.out.println("!!! Authority is: '" + optUserRegistration.get().getAuthority().getAuthority());
        uRepo.delete(optUserRegistration.get());
        return true;
    }
    return false;
}

The UserRegistrationclass looks like this:

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "users")
public class UserRegistration {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
@JsonIgnore
private String password;
@JsonIgnore
private boolean enabled;
private Integer profilePicture;
private String email;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id")
private Authority authority;
@JsonManagedReference
@ManyToMany
@JoinTable(name = "userhasfavorite", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "channelid"))
private List<Channel> favorites;

public UserRegistration() {
}

public UserRegistration(String email, String username, String password, boolean enabled, int profilePicture) {
    this.email = email;
    this.username = username;
    this.password = password;
    this.enabled = enabled;
    this.profilePicture = profilePicture;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public boolean getEnabled() {
    return enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

public Integer getProfilePicture() {
    return profilePicture;
}

public void setProfilePicture(Integer profilePicture) {
    this.profilePicture = profilePicture;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Authority getAuthority() {
    return authority;
}

public void setAuthority(Authority authority) {
    this.authority = authority;
}

public List<Channel> getFavorites() {
    return favorites;
}

public void setFavorites(List<Channel> favorites) {
    this.favorites = favorites;
}
}

The Authorityclass looks like this:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "authorities")
public class Authority {

    @Id
    @Column(name = "userid")
    private int userID;
    private String username;
    private String authority;
    @OneToOne(mappedBy = "authority")
    private UserRegistration user;

    public Authority() {
    }

    public Authority(int userID, String username, String authority) {
        this.userID = userID;
        this.username = username;
        this.authority = authority;
    }

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

    public UserRegistration getUser() {
        return user;
    }

    public void setUser(UserRegistration user) {
        this.user = user;
    }
}

Thanks a lot for reading this. Please ask me for more details if required.

In order for cascading to work; you need to reverse the entity relation definition. The cascading only works for parent entity to child entity. What I mean is the following. Please define authority in UserRegistration as:

@OneToOne(mappedBy="user", cascade = CascadeType.ALL)
private Authority authority;

and the userRegistration in Authority as:

@OneToOne
@MapsId
private UserRegistration user;

I believe this should work. Take a look at the following for more information. Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM