我嘗試從存儲庫中使用'刪除'從實體中刪除對象,但有些東西不起作用。Spring Hibernate刪除對象
@Entity
public class Director implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(length = 15)
private String name;
@Column(length = 40)
private String surname;
@Column(length = 40,unique=true, nullable=false)
private String email;
@Column(length = 12)
private String phone;
@OneToOne
private School school;
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
庫:
@RequestMapping(value = "deleteDirector", method = RequestMethod.POST)
public String delpost(@ModelAttribute Director director) {
try {
directorRepository.delete(director.getId());
return "deleteDirectorSuccess-view";
}
catch(Exception e){
}
return "deleteDirectorError-view";
}
我不知道會發生什麼,因爲有它返回deleteDirectorSuccess-view
但不會刪除該數據庫對象(IM:
@Repository
public interface DirectorRepository extends CrudRepository<Director, Long> {
Director findOneByEmail(String email);
}
從控制器的方法確定id是正確的,我試着把directorRepository.delete(ID_FROM_DATABASE_AS_LONG);
,但也有同樣的問題。 如果我把腳本放在數據庫中,如:
DELETE FROM public.director
WHERE id=ID_FROM_DATABASE;
它的工作原理,在控制器的另一種方法,我通過保存對象數據庫:
directorRepository.save(director);
完美的作品了。
我不知道如何檢查它爲什麼是錯誤的以及如何解決我的問題。
不確定爲什麼保存起作用。你有沒有在任何地方使用@Transactional註釋? –
不,我沒有@Transactional在我的項目 – barmi
嘗試在delPost方法上使用它,看它是否有效。 –