Issue
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
This behavior is related to the cascade param of ManyToOne annotation,
remove cascade = CascadeType.ALL
from categorylocation attribute will resolve the problem.
Answered By - Bessem Manita
Answer Checked By - Mary Flores (JavaFixing Volunteer)