2014-09-22 49 views
0

我目前正在使用Spring-MVC和hibernate。我在數據庫,表1和表2中有2個表。 Table1與table2有一對多的關係。當我使用查詢從Table1及其Table2中的子項中刪除一行的應用程序時,出現錯誤,表示關係Table1_table2不存在。關係table1_table2不存在Postgres

Code : 
@Table(name="table1") 
class user{ 
    @OneToMany 
    public Set<Accounts> accounts; 
    //Remove method 
    Query query = session.createQuery("From User as u LEFT JOIN FETCH u.accounts WHERE u.id="+id) 
    //then I use a for loop to go through the Accoutns and remove the accounts. 
} 
@Table(name="Table2") 
class accounts{ 
    @manyToOne 
    public User user; 
} 

回答

2

@oneToMany是單向關係 所以你只能使用JPA做到這一點。 所以你只需要用你的oneToMany替換下面的行。

@OneToMany(mappedBy="user",cascade = CascadeType.ALL, orphanRemoval = true) 
+0

我剛開始再做一次。會做的。謝謝。 – 2014-09-23 06:57:41

+0

歡迎大哥 – 2014-09-23 08:39:57