-1

I have a TwinColSelect I'm trying to select via code from the values, but it just never does.

List<Role> list = new RoleJpaController(DataBaseManager.getEntityManagerFactory()).findRoleEntities();
BeanItemContainer<Role> roleContainer = new BeanItemContainer<>(Role.class, list);
TwinColSelect roles = new TwinColSelect(TRANSLATOR.translate("general.role"));

roles.setContainerDataSource(roleContainer);
roles.setRows(5);
roles.setLeftColumnCaption(TRANSLATOR.translate("available.roles"));
roles.setRightColumnCaption(TRANSLATOR.translate("current.roles"));
roles.setImmediate(true);

list.forEach(r -> {
    roles.setItemCaption(r, TRANSLATOR.translate(r.getDescription()));
});

if (user.getUserHasRoleList() != null) {
    Set<Role> rs = new HashSet<>();
    user.getUserHasRoleList().forEach(uhr -> {
        if (uhr.getProjectId() == null) {
            LOG.log(Level.INFO, "Selecting role: {0}", uhr.getRole());
            rs.add(uhr.getRole());
        }
    });
    roles.setValue(rs);
}  

I did verify by debugging that the logic to select roles is executed.

Any idea?

Morfic
  • 15,178
  • 3
  • 51
  • 61
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106
  • One possibility is that there are 2 different `Role` instances: 1 from `findRoleEntities()` and one from `user.getUserHasRoleList().forEach(...)`. Do you have ownership of the `Role` implementation or is it a 3rd party implementation (eg: Spring)? If it's yours, how are `equals()` and `hashCode()` implemented? – Morfic Jun 20 '17 at 08:57
  • I checked via debugging and they point to the same roles. The user-role relationship is many to many. The equals just look at the ids being the same. Same with hashCode. – javydreamercsw Jun 20 '17 at 14:14
  • Then it should be fine. Can you provide a [sscce](http://sscce.org) to reproduce your issue? No DB, no sensitive data, just the basic classes and some fake data required to get the same behaviour as in your description. – Morfic Jun 20 '17 at 14:43
  • I tried to do this and I was unable to make it behave the same on the SSCCE. I'll keep investigating. – javydreamercsw Jun 20 '17 at 18:45

1 Answers1

0

The issue was a bug in the equals method of the Role class. After fixing it it works fine.

javydreamercsw
  • 5,363
  • 13
  • 61
  • 106