Hi,
I have a simple oneToMany relationship which I am trying to map with annotations. I have two tables, ACCOUNT and USER, and two model objects, Account and User. Account has a set of User objects which are related to the account by an account_id foreign key. My schema looks like this -
Code:
ACCOUNT (ID)
USER(ID, ACCOUNT_ID, USERNAME)
Where ACCOUNT_ID is an fk referencing ACCOUNT(ID)
My Account class looks like this -
Code:
@Entity
@Table(name="ACCOUNT")
public class Account {
@Id
@Column(name="ID")
private int id;
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
@Column(name="ACCOUNT_ID", nullable = false)
private Set<User> users;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
....
..........
}
My User class -
Code:
@Entity
@Table(name="USER")
public class User implements UserDetails {
@Id
@Column(name="ID")
private int id;
@ManyToOne
@JoinColumn(name="ACCOUNT_ID")
private Account account;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
...
........
}
I am using the Spring HibernateTemplate in a Dao object to save, read, ect... heres my save method -
Code:
....
public void saveAccount(final Account account) {
hibernateTemplate.saveOrUpdate(account);
}
.....
I am running an integration test which simply creates a new account, a new user, adds that user to the account object and saves the account object. When I call saveAccount from my intgration unit test the account and user objects are persisted. However, the field ACCOUNT_ID in the USER table remains NULL, the reference between USER and ACCOUNT is not maintained.
Can anyone tell me what I have wrong in my annotation mappings? I can't understand why such a simple relationship isn't working.
Thanks in advance :-)
John