Reputation: 5311
I am currently working using Spring-MVC and hibernate. I have 2 tables in database, table1 and table2. Table1 has oneToMany relationship with table2. When I run the application with a query to delete a row from a Table1 and its children from Table2, I get an error saying, relation Table1_table2 does not exist.
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;
}
Upvotes: 0
Views: 1121
Reputation: 6759
@oneToMany
is unidirectional relationship
so you can do it using JPA only.
So just you need to replace following line with your oneToMany.
@OneToMany(mappedBy="user",cascade = CascadeType.ALL, orphanRemoval = true)
Upvotes: 2