Trying to populate combobox from referenced container - not working

Hi Guys,

I am really struggling to get referencing from one container to another working.
I have a grid where I select a user role, I want to then press a button and then bring up a popup which will show the select user as read only (working) and the role selected in a dropdown with all possible roles.
The reference is working (I know because it breaks if I change what I have) but always brings back item id for the first role in the roles table rather than the referenced role id from the grid selection.
Ther eis no way to select items by value, only by item id it seems so stuck.

Sorry know it is a bit long but here is the code, this is where I try get the item id for the reference role ‘Object roleId = userContainer.getReferencedItemId(userRoleId, rolesContainer);’ and this is where I try set it ‘userRoleCombo.setValue(roleId);’

This is the logged output where ‘other role’ is what I want selected in the roles container but is defaulting to the first row ‘admin’

INFO: Roleeeeeeeeeeeeeeeeeeeee1 other role
Feb 13, 2016 9:59:12 AM com.logs.template.UserPermissionsView buttonClick
INFO: Roleeeeeeeeeeeeeeeeeeeee2 admin

public class UserPermissionsView extends VerticalLayout implements View, ClickListener {

private static final long serialVersionUID = 1L;
private boolean initialized;
private Grid userRolesGrid;
private SQLContainer userContainer;
private FreeformQuery userQuery;
private FreeformQuery rolesQuery;
private SQLContainer rolesContainer;
SimpleJDBCConnectionPool connectionPool;
private Button editUserRolesButton;
private FormLayout editUserRolesForm;
private PopupView editUserRolesPopup;
private TextField userNameField;

private TextField testField;
private ComboBox userRoleCombo;
private Button saveButton;
private Button deleteButton;
private Button cancelAddButton;
HorizontalLayout popupButtonsLayout;
Label userRolesPopupTitle;

private void build() {
     connectionPool = MyVaadinApplication.getConnectionPool();
            
    userQuery = new FreeformQuery("SELECT r.role_id, u.user_name, u.user_id, r.role_name  " +

"FROM users u " +
"LEFT OUTER JOIN user_roles ur ON ur.user_id = u.user_id " +
"LEFT OUTER JOIN roles r ON r.role_id = ur.role_id " +
“ORDER BY u.user_name, r.role_name”, connectionPool);
try {
userContainer = new SQLContainer(userQuery);
} catch (SQLException e) {
throw new RuntimeException(e);
}

    userContainer.setAutoCommit(true);
    
    userRoleCombo = new ComboBox("Select User Role");
    userRoleCombo.setRequired(true);    
    
    rolesQuery = new FreeformQuery("SELECT role_id, role_name FROM roles", connectionPool);
    try {
        rolesContainer = new SQLContainer(rolesQuery);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    rolesContainer.setAutoCommit(true);
    userContainer.addReference(rolesContainer, "role_id", "role_id");
    //rolesContainer.addReference(userContainer, "role_id", "role_id");        
    
    userRoleCombo.setContainerDataSource(rolesContainer);
    userRoleCombo.setItemCaptionPropertyId("role_name");
    userRoleCombo.setNullSelectionAllowed(true);
    userRoleCombo.setImmediate(true);
    
    //org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
    userNameField = new TextField("User");
    userNameField.setReadOnly(true);
    testField = new TextField("test");
    
    userRolesGrid = new Grid();    
    userRolesGrid.setContainerDataSource(userContainer);
    //rolesGrid.setSelectable(true);
    //rolesGrid.setMultiSelect(false);
    //userGrid.setEditorEnabled(true);
    addComponent(userRolesGrid);
    editUserRolesButton = new Button("Edit User Roles",  this);
    editUserRolesButton.addStyleName("menubutton");
    editUserRolesButton.setWidth(100, Unit.PIXELS);
    saveButton  = new Button("Save User Roles",  this);
    cancelAddButton  = new Button("Cancel",  this);
    deleteButton  = new Button("Delete User Role",  this);
    deleteButton.setWidth(100, Unit.PIXELS);
    deleteButton.addStyleName("menubutton");
    addComponent(editUserRolesButton);
    addComponent(deleteButton);
    //invalidPassword = new Label("Invalid username or password");
    editUserRolesForm = new FormLayout();
    popupButtonsLayout = new HorizontalLayout();
    userRolesPopupTitle = new Label("Edit User Roles");
    editUserRolesForm.addComponent(userRolesPopupTitle);
    userRolesPopupTitle.addStyleName("popupTitle");
    editUserRolesForm.addComponent(userNameField);
    editUserRolesForm.addComponent(testField);
    editUserRolesForm.addComponent(userRoleCombo);
    popupButtonsLayout.addComponent(saveButton);
    popupButtonsLayout.addComponent(cancelAddButton);
    editUserRolesForm.addComponent(popupButtonsLayout);
    popupButtonsLayout.setSpacing(true);
    //invalidPassword.setVisible(false);
    editUserRolesPopup = new PopupView(null,editUserRolesForm);
    editUserRolesPopup.setHideOnMouseOut(false);
    addComponent(editUserRolesPopup);
    setComponentAlignment(editUserRolesPopup, Alignment.TOP_CENTER);
    FieldGroup fieldGroup = userRolesGrid.getEditorFieldGroup();
    fieldGroup.addCommitHandler(new FieldGroup.CommitHandler() {
        @Override
        public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
            System.out.println("PreCommit");
        }

        @Override
        public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
            Item editedItem = commitEvent.getFieldBinder().getItemDataSource();
            System.out.println("PostCommit ("+editedItem+")");
            refresh();
        }
    });
}

public void refresh() {
    userContainer.refresh();
    rolesContainer.refresh();
    System.out.println("Time data refreshed");
}

@Override
public void enter(ViewChangeEvent event) {
    if (!initialized) {
        build();
        initialized = true;
    }        
}

@Override
public void buttonClick(ClickEvent event) {
    if (event.getButton() == editUserRolesButton) { // ------------------------EDIT
        Logger.getLogger(getClass().getCanonicalName()).log(Level.INFO,
                "Edit User Roles button pressed");
        if (userRolesGrid.getSelectedRow() == null) {
            Notification.show("Select a User");
            return;
        }
        Object userRoleId = userRolesGrid.getSelectedRow();

        userNameField.setReadOnly(false);
        userNameField.setValue((String) userContainer.getContainerProperty(userRoleId, "user_name").getValue());
        userNameField.setReadOnly(true);
        Logger.getLogger(getClass().getCanonicalName()).log(Level.INFO,
                "Roleeeeeeeeeeeeeeeeeeeee1 " + userContainer.getContainerProperty(userRoleId, "role_name").getValue());
        //userRole.setValue(userContainer.getContainerProperty(id, "role_name").getValue());        
        
        Object roleId = userContainer.getReferencedItemId(userRoleId, rolesContainer);
        
        Logger.getLogger(getClass().getCanonicalName()).log(Level.INFO,
                "Roleeeeeeeeeeeeeeeeeeeee2 " + rolesContainer.getContainerProperty(roleId, "role_name").getValue());
        userRoleCombo.setValue(roleId);
        //rolesContainer.setReferencedItem(roleId, userRoleId, userContainer);
        
        editUserRolesPopup.setPopupVisible(true);
        
        
        //userNameField.focus();
    } else if (event.getButton() == saveButton) { // ------------------------EDIT
        Logger.getLogger(getClass().getCanonicalName()).log(Level.INFO,
                "Save User Roles button pressed");
        //editUserRolesPopup.setPopupVisible(true);
        //userNameField.focus();
    }
}

}