Ananasi wrote:
Pass actual User and Role objects to a constructor instead of passing just the foreign keys, and then construct it using those objects.
Code:
public UserRole(User user, Role role, Date dateEntered) {
this.user = user;
user.getUserRoles.add(this);
this.role = role;
role.getUserRoles.add(this);
this.dateEntered = dateEntered;
}
Ok, I know that, but I'm want to avoid extra coding. With this solution I need to read user and role object by my self. In first example:
Code:
UserRole u = getById(UserRole.class, userId, roleId);
User user = u.getUser();(*)
hibernate will return user assigned to this UserRole class and this is OK,
hibernate do all reading, and in this example you posted, I have to read it and add it, and I want to avoid that.
Is there a way that hibernate do that for me as in (*) example?
It is weird that after:
Code:
UserRole u = getById(UserRole.class, userId, roleId);
User user = u.getUser();(*)
and after this:
Code:
UserRole u = new UserRole(userId, roleId, dateEntered);
this.insert(u);
User user = u.getUser();(**)
or
Code:
UserRole u = new UserRole(userId, roleId, dateEntered);
this.insert(u);
UserRole u = getById(UserRole.class, userId, roleId);
User user = u.getUser();(***)
user object returned from (*) is realy User object, and
user object returned from (**) and (***) is null?
Note than in (*) and (***) I have same code for reading userRole and user object!!