以下是我的代码。当我删除oneToMany关系中的跟踪表记录时,我想删除tracking_event的所有记录
跟踪实体
@Entity
@Table(name = "TRACKING")
public class Tracking {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TRACKING_ID")
private int id;
@JsonIgnore
@OneToMany(mappedBy = "tracking",cascade = CascadeType.REMOVE)
private List<TrackingEvent> trackingEvents;
}
TrackingEvent 实体
@Entity
@Table(name = "TRACKING_EVENT")
public class TrackingEvent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TRACKING_EVENT_ID")
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TRACKING_ID")
private Tracking tracking;
}
TrackingRepository 界面
public interface TrackingRepository extends CrudRepository<Tracking, Integer> {
@Modifying
@Transactional
@Query(" DELETE FROM Tracking WHERE id = :tid")
void deleteByUpdatedDate(@Param("tid") int tid);
}
我也尝试过使用
@OneToMany(mappedBy = "tracking",cascade = CascadeType.REMOVE,orphanRemoval = true)
private List<TrackingEvent> trackingEvents;`