gpt4 book ai didi

Spring Data JPA/Hibernate OneToMany 删除 ManyToOne Child

转载 作者:行者123 更新时间:2023-12-01 06:30:27 25 4
gpt4 key购买 nike

我遇到以下问题:

我有 3 个 Pojo

  • 艺术家
  • 相册
  • 跟踪

  • 艺术家与专辑有一对多关系,专辑与轨道有一对多关系。

    艺术家类
    @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;

    相册类
    @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;

    跟踪类
    @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;
    }

    跟踪服务.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 只是给了我很多选择语句作为输出。

    任何帮助表示赞赏,如果您需要我提供更多信息,请随时询问。

    最佳答案

    在您的存储库方法中,添加此

    @Query("delete  from Track t where t.id=:id") 
    void delete(@Param("id")String id);

    由于您没有覆盖级联策略,因此默认级联策略为无,我相信这会起作用。
    引用- http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html_single/

    关于Spring Data JPA/Hibernate OneToMany 删除 ManyToOne Child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616005/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com