I'm experimenting with lazy loading entities in hibernate but could not get over the error above. I'm basically playing with 2 entities: User and Role:
public class User implements Serializeable {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "T_USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
private Set<Role> roles = new HashSet<Role>();
}
public class Role implements Serializeable { }
And then I have an action bean that creates the User entity, let's called this approach 1:
user.getRoles().add(roleService.findByName(RolesEnum.ADMIN.toString()));
em.persist(user);
And then I tried to persist the user entity first before setting the roles and updating but it also failed:
userService.create(user);
User user = userService.findById(user.getId());
Set<Role> roles = new HashSet<Role>();
roles.add(roleService.findByName(RolesEnum.MEMBER.toString()));
user.setRoles(roles);
userService.update(user, true);
Note that:
- userService.create = persist
- userService.update = merge
- MEMBER role already exists in roles table
- I also tried fetching the roles: entity.getRoles();
Below is the complete error log:
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.Role org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)