2011-05-01 136 views
0

我有兩個類:支出和類別。支出可以有多個類別。我希望通過支出(家長)實施所有類別(兒童)級聯保存。但無法實現這一點。在堅持支出記錄沒有分類條目創建。請讓我知道我做錯了什麼。這裏是我的課: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; 
    } 

回答

0

嘗試增加

@ManyToOne(targetEntity=Expenditure.class) 
@JoinColumn(name="parentExpenditureId") // or whatever column name exists in your database schema 
private Expenditure expenditure; 

而且也不會在你的實體類混合起來的字段和屬性的訪問類型。

+0

感謝Satadru的回覆..我能夠通過將標識符註釋轉移到支出類中的字段級別來解決我的問題。它沒有添加JoinColumn(我認爲是可選的)。但是,我不確定原因! – Sumit 2011-05-01 18:45:01