【发布时间】:2018-05-22 18:17:19
【问题描述】:
我有一个问题要解决。我有两个实体:Action 和 Logging:
@Entity
public class Action {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
...other fields
...other fields
@Column
private Date start;
@Column
private Date end;
@Entity
public class Logging {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name = "action_id")
private Action action;
..not important fields
@Column
private String domain;
@Column
private Date date;
只有 Logging 表具有 action_id 列作为 FOREIGN KEY。
一个动作可以有多个日志记录。
问题是:如何删除日期早于某个日期并具有某个域(位于 Logging 表中)的所有 Action 实体? 例如
deleteActionByDateAndDomain(LocalDateTime date, String domain)
记录实体是所有者,对吗?那么,我应该删除 Action 实体,然后删除所有相关的 Logging 实体,例如 CASCADING 吗?或者相反 - 首先删除所有满足我条件的 Logging 实体(date before dateX and domain == myDomain 作为参数传递)?
您将如何实现它? 例如,我可以删除 Action 实体和所有相关的 Logging 实体,但 Action 实体中没有字段域(仅在 Logging 中)。
如果可能的话,我应该使用 CrudRepository 接口。
非常感谢您!
【问题讨论】:
标签: java hibernate jpa persist