开发者

@ElementCollection, @CollectionTable and Enum - Strange delete/insert behavior

@Entity
public class User{

    @ElementCollection
    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "SEC_USER_ROLES",
    joinColumns =
    @JoinColumn(name = "USER_ID", referencedColumnName = "ID"))
    @Column(name = "ROLE_NAME")
    private List<Role> roles;

    [...]

}

public enum Role {

    ROLE_SUPER_ADMIN,
    ROLE_ADMIN,
    ROLE_ARB,
    ROLE_AP;

    [...]
}

With this mapping, when I try do delete one ROLE, for example ROLE_ARB, it always ends up with deleting the role and inserting it once again.

DELETE FROM SEC_USER_ROLES WHERE ((USER_ID = ?) AND (ROLE_NAME = ?))

    bind => [9451, ROLE_ADMIN]

INSERT INTO SEC_USER_ROLES (USER_ID, ROLE_NAME) VALUES (?, ?)

    bind => [9451, ROLE_ADMIN]

I tried to solve the problem with @OrderColumn (name="USER_ID") but then the mapping of the User_id is not correct.

Any idea would be appreciated.

The Roles are represented as selectManyCheckbox The ManagedBean prepares the entity (User)

   ...
  List<String开发者_运维知识库> selectedroles = this.getSelectedItems();
  List<Role> newroles = new ArrayList<Role>();
  if (selectedroles != null) {
  for (String r : selectedroles) {
  newroles.add(Role.valueOf(r));

            }
  getEntity().setRoles(newroles);
   ...    
  security.save(getEntity());

and the EJB makes updates if it is an existing Entity

  EntityManager em;
    ...
  this.em.merge(user);

So when someone deselects all (previous selected) Roles there is always one Role left in the database which is not deleted, because of the delete/insert behavior I described before.


@OrderColumn solved the problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜