1

I implemented a workbench permission system with groups and permissions. A reference table workbench_group_permissions_reference has references so I can easily add and remove permissions to a group. Adding a new reference entry works fine, but removing does not. I do not get any error, but the reference still exists in the database after removal. I am using postgreSQL.

Here is my reference class:

@XmlRootElement
@Entity
@Table(name = "workbench_group_permissions_reference", uniqueConstraints = {
        @UniqueConstraint(columnNames = { "workbenchgroupspermissions_id", "workbench_groups_id" }) })
public class WorkbenchGroupPermissionReferenceEntity extends BasicEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne
    @JoinColumn(name = "workbenchgroupspermissions_id")
    private WorkbenchPermissionEntity workbenchPermission;

    @ManyToOne
    @JoinColumn(name = "workbench_groups_id")
    private WorkbenchGroupEntity workbenchGroup;

    /**
     * Empty constructor to make JPA happy.
     */
    public WorkbenchGroupPermissionReferenceEntity() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public WorkbenchPermissionEntity getWorkbenchPermission() {
        return workbenchPermission;
    }

    public void setWorkbenchPermission(WorkbenchPermissionEntity workbenchPermission) {
        this.workbenchPermission = workbenchPermission;
    }

    public WorkbenchGroupEntity getWorkbenchGroup() {
        return workbenchGroup;
    }

    public void setWorkbenchGroup(WorkbenchGroupEntity workbenchGroup) {
        this.workbenchGroup = workbenchGroup;
    }
}

This is my remove method:

public void deleteWorkbenchGroupPermission(final WorkbenchGroupPermissionReferenceEntity workbenchGroupPermission) {
    long id = workbenchGroupPermission.getId();
    super.delete(WorkbenchGroupPermissionReferenceEntity.class, id);
}

And the super.delete method:

protected void delete(final Class<?> type, final Object id) {
    Object ref = this.em.getReference(type, id);
    this.em.remove(ref);
}

What am I missing here?

Felix Hagspiel
  • 2,634
  • 2
  • 30
  • 43
  • 1
    share the code for your `super.delete(..)` method. We have no idea what it does – Ranjeet Jul 02 '17 at 06:24
  • 1
    Might be the reason: https://stackoverflow.com/questions/34840903/silently-ignored-remove – Dragan Bozanovic Jul 02 '17 at 15:12
  • @Dark added the missing super method – Felix Hagspiel Jul 02 '17 at 16:33
  • 1
    Is your methos called inside a transaction? Is the transaction properly committed? Why do you take an entity, extract its ID, get a reference from this ID (thus getting back the entity you got in the first place), and then delete the entity, instead of just deleting the entity you have from the beginning? – JB Nizet Jul 02 '17 at 16:41
  • @JBNizet this is due to the setup of the application. No doubt that there is some refactoring to do :) And yes, it is called inside an transaction & it is properly commited – Felix Hagspiel Jul 02 '17 at 16:47

1 Answers1

0

The link in the comment of Dragan was pointing me to the right direction. I tried to delete the reference, but the owning entity (WorkbenchGroupEntity) still had the reference. What I did to solve the problem was to first remove the reference from the owning entity and then remove the cross-reference entity afterwards:

    WorkbenchGroupPermissionReferenceEntity permissionToRemove = new WorkbenchGroupPermissionReferenceEntity();
    for(WorkbenchGroupPermissionReferenceEntity permissionReference : existingGroupPermissions) {
        Long permissionRefernceId = permissionReference.getId();
        if(permissionRefernceId.equals(permissionId)){
            permissionToRemove = permissionReference;
        } else {
            newGroupPermissions.add(permissionReference);
        }
    }
    workbenchGroupEntity.setWorkbenchGroupPermissions(newGroupPermissions);
    workbenchGroupControl.updateWorkbenchGroup(workbenchGroupEntity);
    workbenchGroupPermissionsControl.deleteWorkbenchGroupPermission(permissionToRemove);

EDIT: As the solution mentioned above was the main reason for the delete not working properly, I made another stupid mistake: I was comparing the id of the cross reference entity to the id of the permission id instead of the permission id itself, so the reference was never found.

Updated check:

    for (WorkbenchGroupPermissionReferenceEntity permissionReference : existingGroupPermissions) {
        // here was the mistake
        Long permissionReferenceId = permissionReference.getWorkbenchPermission().getId();
        if (permissionReferenceId.equals(permissionId)) {
            permissionToRemove = permissionReference;
        } else {
            newGroupPermissions.add(permissionReference);
        }
    }
Felix Hagspiel
  • 2,634
  • 2
  • 30
  • 43