我得到依赖实体的违反约束异常,但它只发生在旧记录上。我最后的更改是向名为kbibusiness的父实体添加mappedBy=kpi
-属性。
我可以删除新创建的具有依赖关系的父实体,但不能删除旧实体。
kpibusiness.java:网址:
@Entity
public class KPIBusiness extends KPI {
private Long businessProcessId;
private String businessProcess;
private Long eventId;
private String event;
@OneToMany(mappedBy="kpi", fetch = FetchType.EAGER, cascade=CascadeType.ALL)
private Set<Dependency> dependencies;
}
依赖关系.java
@Entity
@Table(name="dependencies")
public class Dependency {
@Id
@GeneratedValue
private Long did;
public Long getDid() {
return did;
}
public void setDid(Long did) {
this.did = did;
}
private AlertConstants.TRIGGER kpiType;
private Long kpiId;
@JsonIgnore
@ManyToOne
@JoinColumn(name="id", nullable=false)
private KPIBusiness kpi;
}
例外情况:
原因:org.h2.jdbc.jdbcsqlException:referentielle integrit_t
Verletzt:“FKSKW30595DULXL90POB2K9KT3I:电话:
public.kpi依赖项外键(依赖项)
引用public.dependencies(did)(33)”
引用完整性约束冲突:“fkskw30595dulxl90pob2k9kt3i:public.kpibusiness_dependencies foreign
键(依赖项)引用public.dependencies(did)(33)”;sql
声明:
从依赖项中删除哪里有=?[23503-196]
我用的是这个代码:
repository.delete(currentKPI);
repository.flush();
现在我正在考虑使用这个代码:
currentKPI.setDependencies(new HashSet<Dependency>());
currentKPI = repository.save(currentKPI);
repository.flush();//HACK delete at first try
repository.delete(currentKPI);
repository.flush();
但是,似乎
currentKPI.setDependencies(dependencies);
根本不会更新依赖项。我使用此代码尝试更改依赖项:Set<Dependency> dependencies = new HashSet<Dependency>();
for(RemoteKPINames kpi : tcsDependencies.getValue()) {
Dependency dependency = new Dependency();
dependency.setKpiId(kpi.getId());
dependency.setKpiType(kpi.getType());
dependency.setKpi(data);
dependencies.add(dependency);
}
log.info("Setting dependencies in data object #={}", dependencies.size());
data.setDependencies(dependencies);
最佳答案
employeeEntity.java公司
@Entity
@Table(name = "Employee")
public class EmployeeEntity implements Serializable
{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer employeeId;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Set<AccountEntity> accounts;
//Getters and Setters Ommited
}
accountentity.java公司
@Entity
@Table(name = "Account")
public class AccountEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer accountId;
@Column(name = "ACC_NO", unique = false, nullable = false, length = 100)
private String accountNumber;
@OneToOne (mappedBy="accounts", fetch = FetchType.LAZY)
private EmployeeEntity employee;
}
查看上面employeeentity.java源代码中的粗体行。它定义了“cascade=cascadeType.all”,本质上意味着在employeeEntity上发生的任何更改也必须层叠到accountEntity。如果保存员工,则所有关联的帐户也将保存到数据库中。如果删除员工,则与该员工关联的所有帐户也将被删除。很简单。
但是如果我们只想级联保存操作而不是删除操作呢?然后我们需要使用下面的代码清楚地指定它。
@OneToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Set<AccountEntity> accounts;
现在只有当使用employee实例调用save()或persist()方法时,才会持久化帐户。如果在会话中调用任何其他方法,则其效果不会影响/层叠到帐户。
JPA级联类型
Java持久性体系结构支持的级联类型如下:
cascadeType.persist:表示save()或persist()操作
级联到相关实体。
cascadeType.merge:表示当
所属实体已合并。
cascadeType.refresh:对refresh()执行相同的操作
操作。
cascadeType.remove:删除与
删除所属实体时的此设置。
cascadeType.detach:如果“手动
“分离”发生。
cascadeType.all:是上述所有cascade的缩写。
操作。