我遇到了以下问题:
我有3个Pojo的
艺术家与专辑有OneToMany关系,而专辑与专辑有OneToMany关系。
Artist.class
@Entity
public class Artist {
@Id
@GeneratedValue
@Column(name = "artist_id")
private long artist_id;
@Column(name = "artist_name", nullable = false, length = 32)
private String artist_name;
@Column(name = "artist_bio", nullable = false, length = 255)
private String artist_bio;
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name = "artist_avatar")
private byte[] artist_avatar;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "artist", orphanRemoval = true)
private List<Album> albumList;
Album.class
@Entity
public class Album {
@Id
@GeneratedValue
@Column(name = "album_id")
private long album_id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "artist_id", nullable = false)
private Artist artist;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "album", orphanRemoval = true)
private List<Track> trackList;
@Column(name = "album_name", nullable = false, length = 32)
private String album_name;
@Column(name = "album_description", nullable = false, length = 255)
private String album_description;
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name = "album_avatar")
private byte[] album_avatar;
Track.class
@Entity
public class Track {
@Id
@GeneratedValue
@Column(name = "track_id")
private long track_id;
@Column(name = "track_no", nullable = false, length = 2)
private String track_no;
@Column(name = "track_name", nullable = false, length = 32)
private String track_name;
@Column(name = "track_length", nullable = false, length = 32)
private String track_length;
@Column(name = "track_file", nullable = false, length = 128)
private String track_file;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "album_id", nullable = false)
private Album album;
跟踪Controller.class
@RequestMapping(value = "/track", params = "delete", method = RequestMethod.POST)
@Transactional
public String postDeleteTrack(@RequestParam Long album_id, Long track_id) {
trackService.deleteTrack(track_id);
return "redirect:album/" + album_id;
}
跟踪Service.class
public void deleteTrack(Long track_id) {
trackRepository.delete(track_id);
}
TrackRepository.class
@Repository
public interface TrackRepository extends CrudRepository<Track, Long>{
}
当我调用trackService.deleteTrack(track_id)时;我的MySQL数据库中的轨道未被删除。 Hibernate只给了我很多select语句作为输出。
感谢任何帮助,如果您需要我提供更多信息,请随时提出。
答案 0 :(得分:0)
在您的存储库方法中,添加此
@Query("delete from Track t where t.id=:id")
void delete(@Param("id")String id);
由于你没有覆盖级联策略,默认的级联策略是none,我相信这会有效。 reference- http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html_single/