@ManyToOne关系仅删除父母

时间:2018-07-23 23:39:23

标签: java hibernate hibernate-mapping

我有两个实体保留和交往。当我从预订中删除行时,我不想删除Seance

@Entity
@Table(name="reservation")
@Data
@NoArgsConstructor
public class Reservation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, unique = true)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//   / @OnDelete(action = OnDeleteAction.CASCADE)
//    @JoinColumn(name = "id_seance")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Seance reservationSeance;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private User userReservation;

    @OneToMany(orphanRemoval = true)
    private List<Seat> seats = new ArrayList<>();
}

和我的Seance实体

@Entity
@Table(name="seance")
@Data
public class Seance {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, unique = true)
    private Integer id;

    @Column(length=127)
    private String kind;

//    @ManyToMany(mappedBy = "seances")
//    @JsonIgnore
//    private List<Repertoire> repertoires = new ArrayList<>();

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "hall_id")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Hall hall;
}

删除保留时,我不删除seance子行需要做什么。我应该更改映射还是其他一些技巧?实体Seance也与具有关系@ManyToOne的曲目表结合在一起,并休眠创建链接表repertoire_seance的表。当我尝试删除预订时发生错误

Cannot delete or update a parent row: a foreign key constraint fails (`todo`.`repertoire_seance`, CONSTRAINT `FKani6veaxbp3vxruqjivg9o9sp` FOREIGN KEY (`seance_id`) REFERENCES `seance` (`id`))

0 个答案:

没有答案