'Spring boot; When I delete the data, it deletes all the data connected to that foreign key
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;
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 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
}
Solution 1:[1]
This behavior is related to the cascade param of ManyToOne annotation,
remove cascade = CascadeType.ALL from categorylocation attribute will resolve the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bessem Manita |
