Hibernate: la colección no se vuelve a llenar después de guardar la unión de muchos a muchos

I just need to figure out why my collection of Roles would not populate member properties after save (add). I have two tables, User and Role, and the join table UserRole.

public class User implements Serializable {
private int id;
@NotBlank(message = "{User.userName.notBlank}")
@AlphaNumeric(message = "{User.userName.alphaNumeric}")
private String name;
@NotBlank(message = "{User.password.notBlank}")
@AlphaNumeric(message = "{User.password.alphaNumeric}")
private String password;
private boolean enabled;
private boolean deleteSafe; 

private Set<Role> roles; <-- this is my problem

And here is my Hibernate XML mapping

<class name="User">
    <id name="id">
        <generator class="identity" />
    <property name="name"></property>
    <property name="password"></property>
    <set name="roles" table="UserRoles" lazy="false">
        <key column="userId"></key>
        <many-to-many column="roleId" class="Role"></many-to-many>

And here is the simple Role class

public class Role implements Serializable {
private int id; 
private String authority;
private String name;

My UserRole table consists only of two columns, userId which maps to the id of User, and roleId, which maps to the id in Role.

I pass a User object with name, password, and a set of Roles. Each Role in this list only has the id populated. The UserRole table gets saved properly, but I want that after saving, the Set in my User object will have the authority and name attributes populated.

Those properties are populated in a normal 'get' User using the same mapping, I was hoping it will do the same after it does a 'save'.

preguntado el 31 de julio de 12 a las 11:07

1 Respuestas

hibernate won't do that. You basicly have a broken model here and hibernate can not distinguish between a Role with password set to null and a dummy Role which you want it to populate. If you just want to assign Roles with known ids to the newly created User then use session.load().

for(long roleId : roleIds)
    newUser.getRoles().add(session.load(Role.class, roleId));

the Roles returned by session.Load are proxies which do not generate a roundtrip until they are accessed.

Update: if you know you need the roles later then eager loading them would be better

for(Role role : session.CreateCriteria(Role.class).add(Expression.in("id", roleIds)).list())

Respondido 31 Jul 12, 14:07

Thanks, might as well do it this way. - FrancisWebDev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.