identity manager - duplicate user role mapping created on new user creation
atarshis Aug 19, 2008 4:02 PMWhen creating a new userAccount object using identity manager in seam SVN latest copy, i am getting duplicate userAccount/userRole mappings created when i merge the userAccount object after setting the customer on the userAccount.
My example is basically the same as the seamspace MemberAcount/Member example, where userAccount is MemberAccount and Customer is Member.
Once i remove the merge line (commented out below), i get only a single userAccount/userRole mapping and everything works fine.
Here is the code:
/**
* Create user account, log the user in, and display
*/
public void end()
{
new RunAsOperation() {
public void execute() {
identityManager.createUser(customer.getEmail(), password);
identityManager.grantRole(customer.getEmail(), "user");
}
}.run();
entityManager.persist(customer);
userAccount.setCustomer(customer);
//entityManager.merge(userAccount);
// Login the user
identity.setUsername(userAccount.getUserName());
identity.setPassword(password);
identity.login();
}
@Observer(JpaIdentityStore.EVENT_USER_CREATED)
public void accountCreated(UserAccount account)
{
this.userAccount = account;
}
@Entity
@Table(name = "user_account", catalog = "solarbay")
public class UserAccount implements java.io.Serializable {
private Long userAccountId;
private String userName;
private String passwordHash;
private boolean enabled;
private Set<UserRole> roles = new HashSet<UserRole>(10);
private Customer customer;
private Generator generator;
public UserAccount() {
}
public UserAccount(String userName, String passwordHash) {
this.userName = userName;
this.passwordHash = passwordHash;
}
public UserAccount(String userName, String passwordHash, boolean enabled,
Set<UserRole> roles) {
this.userName = userName;
this.passwordHash = passwordHash;
this.enabled = enabled;
this.roles = roles;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_account_id", unique = true, nullable = false)
public Long getUserAccountId() {
return this.userAccountId;
}
public void setUserAccountId(Long userAccountId) {
this.userAccountId = userAccountId;
}
@UserPrincipal
@Column(name = "user_name", nullable = false, length = 100)
@NotNull
@Length(max = 100)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@UserPassword(hash = "md5")
@Column(name = "password_hash", nullable = true, length = 200)
@Length(max = 200)
public String getPasswordHash() {
return this.passwordHash;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
@UserEnabled
@Column(name = "enabled")
@Type(type = "yes_no")
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@UserRoles
@ManyToMany(targetEntity = UserRole.class)
@JoinTable(name = "user_has_user_role", joinColumns = @JoinColumn(name = "user_account_id"), inverseJoinColumns = @JoinColumn(name = "user_role_id"))
public Set<UserRole> getRoles() {
return this.roles;
}
public void setRoles(Set<UserRole> roles) {
this.roles = roles;
}
/**
* @return the customer
*/
@OneToOne
@JoinColumn(name = "customer_id", nullable = true)
public Customer getCustomer() {
return customer;
}
/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* @return the generator
*/
@OneToOne
@JoinColumn(name = "generator_id", nullable = true)
public Generator getGenerator() {
return generator;
}
/**
* @param generator the generator to set
*/
public void setGenerator(Generator generator) {
this.generator = generator;
}
}
/**
* UserRole generated by hbm2java
*/
@Entity
@Table(name = "user_role", catalog = "solarbay")
public class UserRole implements java.io.Serializable {
private Long userRoleId;
private String name;
private boolean conditional;
private Set<UserRole> groups;
public UserRole() {
}
public UserRole(String name) {
this.name = name;
}
public UserRole(String name, boolean conditional) {
this.name = name;
this.conditional = conditional;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_role_id", unique = true, nullable = false)
public Long getUserRoleId() {
return this.userRoleId;
}
public void setUserRoleId(Long userRoleId) {
this.userRoleId = userRoleId;
}
@RoleName
@Column(name = "name", nullable = false, length = 200)
@NotNull
@Length(max = 200)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@RoleGroups
@ManyToMany(targetEntity = UserRole.class)
@JoinTable(name = "role_group", joinColumns = @JoinColumn(name = "user_role_id"), inverseJoinColumns = @JoinColumn(name = "member_of"))
public Set<UserRole> getGroups() {
return groups;
}
public void setGroups(Set<UserRole> groups) {
this.groups = groups;
}
@RoleConditional
@Column(name = "conditional")
@Type(type = "yes_no")
public boolean isConditional() {
return conditional;
}
public void setConditional(boolean conditional) {
this.conditional = conditional;
}
}