執行我有兩個實體:ID變成「?」當CrudRepository
@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
}
這對於擴展CrudRepository
的Group
實體庫:
public interface GroupRepository extends CrudRepository<Group, Long> {
public Group findById(long 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=?
Hibernate: delete from `group` where id=?
我敢肯定,我通過正確的ID來組信息庫的.delete
方法。
但爲什麼調用'groupRepository.delete(id)'刪除表中的所有項目? –
你可以設置show_sql在休眠配置和顯示日誌,你看到什麼時候刪除 – xyz
'休眠:從'組''刪除其中id =?' 我把代碼中斷點,我敢肯定,傳入組ID不是'null'或格式不正確。 –