如何解决JPA级联持久性不起作用
| 我有两个班级:支出和类别。支出可以有多个类别。我希望用支出(父母)实现所有类别(儿童)的级联保存。但是无法实现这一点。在持久性支出记录上,不会创建类别条目。请让我知道我在做什么错。这是我的课程:@Entity
@Table(name=\"EXPENDITURE\")
public class Expenditure {
private Long id;
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy=\"expenditure\")
private Set<Category> associatedCategories = new HashSet<Category>();
public Expenditure() { }
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name=\"EXPENDITURE_ID\")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void addCategory(Category category)
{
this.associatedCategories.add(category);
category.setExpenditure(this);
}
}
@Entity
@Table(name=\"CATEGORY\")
public class Category {
@Id @GeneratedValue
@Column(name=\"CATEGORY_ID\")
private Long id;
private String name;
@ManyToOne(targetEntity=Expenditure.class)
private Expenditure expenditure;
public Category(){}
public Category(String name)
{
this.name = name;
}
public Category(String name,String description)
{
this.name = name;
}
@Column(name=\"NAME\")
public String getName() {
return name;
}
public Expenditure getExpenditure() {
return expenditure;
}
public void setExpenditure(Expenditure expenditure) {
this.expenditure = expenditure;
}
解决方法
尝试添加
@ManyToOne(targetEntity=Expenditure.class)
@JoinColumn(name=\"parentExpenditureId\") // or whatever column name exists in your database schema
private Expenditure expenditure;
并且不要在实体类中混淆字段和属性访问类型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。