I have two entity,one of them(the Task) own two refers of another entity(The user).
public class Task{
private int id;
private User publisher;
private List<User> manager;
}
public User{
private int id;
private String name;
private List<Task> tasks;
}
At the Task side,I can set "one-to-one" to "publisher",and "one-to-many" to "manager",but how to set the mapping in the user side?
It depends on what you want to have in the database.
If you want to have a separate foreign key for
publisher
and join table formanager
, the easiest way to map the other side it this:If you actaully need a single list of tasks in
User
, you can create it programmatically.Alternatively, you can have a single join table with an additional
Role
property:Also think whether you need this relationship to be bidirectional at all. It looks like there are not so many use cases when you need a list of all tasks where a particular user participate. Perhaps you don't need this relationship at the
User
side at all, and can use queries instead.