Reputation: 2387
I am trying to model users with home directories in my system. I got the following model declarations:
@Entity
public class Directory extends Model {
public String name;
@ManyToOne public Directory parent;
@ManyToOne public User owner;
@OneToMany public Set<User> sharees;
}
@Entity
public class User extends Model {
@Unique @Column(unique=true) public String username;
public String password;
public Directory homeDirectory;
public User(String username, String password) {
this.username = username;
this.password = password;
this.homeDirectory = new Directory(username, null, this);
}
}
When I create a user and call .save(), I get an error (A javax.persistence.PersistenceException has been caught, org.hibernate.exception.GenericJDBCException: could not insert: [models.User]). Can anyone explain why?
Using fixtures, can I test this? I'd need to create forward references in my yaml file, but I'm not sure if that's possible.
Thanks,
Vincent.
Upvotes: 1
Views: 98
Reputation: 9784
@OneToOne
annotation for homeDirectory
.CascadeType.ALL
so these directories automatically get created/deleted when users get created/deleted.Upvotes: 2