Home > Mobile >  Spring boot; When I delete the data, it deletes all the data connected to that foreign key
Spring boot; When I delete the data, it deletes all the data connected to that foreign key

Time:03-18

I have a question.When I delete any data from category table with jpa; It deletes the parent it is connected to in the location table and all the categories that that parent is connected to.

sample;

My Categories Data

if i delete id 113 in category table; It deletes all location_id 1 in category table and locataion with id 1 in location table. As a result, anything with a location_id of 1 is deleted.

My Tables

My Location :

@Getter
@Setter
@Entity
@Table(name = "location_entity")
public class LocationEntity extends BaseEntity {
    // some properties

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(mappedBy = "categorylocation")
    private List<CategoryEntity> categoryEntityList;

    // some properties
}

My Category:

@Getter
@Setter
@Entity
@Table(name = "category_entity")
public class CategoryEntity extends BaseEntity {

// some properties

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "location_id")
    private LocationEntity categorylocation;

// some properties
}

CodePudding user response:

This behavior is related to the cascade param of ManyToOne annotation, remove cascade = CascadeType.ALL from categorylocation attribute will resolve the problem.

  • Related