我有这个自加入实体:
@Entity
@Table(name = "PackageTemplate")
public class PackageTemplate implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique = true, nullable = false)
private Long idPackageTemplate;
private Byte status;
// bi-directional many-to-one association to PackageTemplate
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "packageTemplate")
private PackageTemplate packageTemplate;
// bi-directional many-to-one association to PackageTemplate
@OneToMany(mappedBy = "packageTemplate", cascade = { CascadeType.ALL })
@JoinTable(name = "PackageTemplate", joinColumns = { @JoinColumn(name = "packageTemplate", nullable = true) }, inverseJoinColumns = { @JoinColumn(name = "idPackageTemplate", nullable = false) })
private List<PackageTemplate> packageTemplateList;
public PackageTemplate() {
}
public Long getIdPackageTemplate() {
return this.idPackageTemplate;
}
public Byte getStatus() {
return this.status;
}
public void setStatus(Byte status) {
this.status = status;
}
public PackageTemplate getPackageTemplate() {
return this.packageTemplate;
}
public void setPackageTemplate(PackageTemplate packageTemplate) {
this.packageTemplate = packageTemplate;
}
public List<PackageTemplate> getPackageTemplateList() {
return this.packageTemplateList;
}
public void setPackageTemplateList(List<PackageTemplate> packageTemplateList) {
this.packageTemplateList = packageTemplateList;
}
}
当我更新 packageTemplate 设置 PackageTemplateList 时,它不起作用。相关物品保持与父亲的关系。
没有显示错误,只是级联似乎什么都不做。
任何想法?
解决了, JB Nizet是对的。问题是“packageTemplateList 中的mappedBy”。
代码应该是这样的:
// bi-directional many-to-one association to PackageTemplate
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "packageTemplate")
private PackageTemplate packageTemplate;
// bi-directional many-to-one association to PackageTemplate
@OneToMany
@JoinColumn(name = "packageTemplate")
private List<PackageTemplate> packageTemplateList;
这段代码现在可以工作了。