I have two entities:
@Entity
@Table(name="\"Group\"")
public class Group implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@NotNull
@ManyToOne(cascade=CascadeType.REMOVE, fetch=FetchType.EAGER)
@JoinColumn(name="admin_id", referencedColumnName="id")
private User admin;
// .. other properties + setters and getters
}
-
@Entity
@Table(name="User")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@OneToMany(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER, mappedBy="admin")
private Set<Group> ownedGroups;
// other properties + setters and getters
}
This is the repository for the Group
entity that extends CrudRepository
:
public interface GroupRepository extends CrudRepository<Group, Long> {
public Group findById(long id);
}
When I delete a group, all groups in the db gets deleted. why does that happen?
This is the what I see in the log (I have the same number of entries in my group table as the number of log lines)
Hibernate: delete from `group` where id=?
Hibernate: delete from `group` where id=?
Hibernate: delete from `group` where id=?
Hibernate: delete from `group` where id=?
Hibernate: delete from `group` where id=?
Hibernate: delete from `group` where id=?
I'm sure that I pass the correct id to the .delete
method of the group repository.