@Test
  public void issue42() {
    PlainSchema userId = plainSchemaDAO.find("userId");

    Set<MappingItem> beforeUserIdMappings = new HashSet<>();
    for (ExternalResource res : resourceDAO.findAll()) {
      if (res.getProvision(anyTypeDAO.findUser()) != null
          && res.getProvision(anyTypeDAO.findUser()).getMapping() != null) {

        for (MappingItem mapItem :
            res.getProvision(anyTypeDAO.findUser()).getMapping().getItems()) {
          if (userId.getKey().equals(mapItem.getIntAttrName())) {
            beforeUserIdMappings.add(mapItem);
          }
        }
      }
    }

    ResourceTO resourceTO = new ResourceTO();
    resourceTO.setKey("resource-issue42");
    resourceTO.setConnector(100L);
    resourceTO.setEnforceMandatoryCondition(true);

    ProvisionTO provisionTO = new ProvisionTO();
    provisionTO.setAnyType(AnyTypeKind.USER.name());
    provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME);
    resourceTO.getProvisions().add(provisionTO);

    MappingTO mapping = new MappingTO();
    provisionTO.setMapping(mapping);

    MappingItemTO item = new MappingItemTO();
    item.setIntAttrName("userId");
    item.setIntMappingType(IntMappingType.UserPlainSchema);
    item.setExtAttrName("campo1");
    item.setConnObjectKey(true);
    item.setMandatoryCondition("false");
    item.setPurpose(MappingPurpose.BOTH);
    mapping.setConnObjectKeyItem(item);

    ExternalResource resource = resourceDataBinder.create(resourceTO);
    resource = resourceDAO.save(resource);
    assertNotNull(resource);
    assertNotNull(resource.getProvision(anyTypeDAO.findUser()).getMapping());
    assertEquals(1, resource.getProvision(anyTypeDAO.findUser()).getMapping().getItems().size());

    resourceDAO.flush();

    ExternalResource actual = resourceDAO.find("resource-issue42");
    assertNotNull(actual);
    assertEquals(resource, actual);

    userId = plainSchemaDAO.find("userId");

    Set<MappingItem> afterUserIdMappings = new HashSet<>();
    for (ExternalResource res : resourceDAO.findAll()) {
      if (res.getProvision(anyTypeDAO.findUser()) != null
          && res.getProvision(anyTypeDAO.findUser()).getMapping() != null) {

        for (MappingItem mapItem :
            res.getProvision(anyTypeDAO.findUser()).getMapping().getItems()) {
          if (userId.getKey().equals(mapItem.getIntAttrName())) {
            afterUserIdMappings.add(mapItem);
          }
        }
      }
    }

    assertEquals(beforeUserIdMappings.size(), afterUserIdMappings.size() - 1);
  }
 @Override
 public Response create(final ResourceTO resourceTO) {
   ResourceTO created = logic.create(resourceTO);
   URI location = uriInfo.getAbsolutePathBuilder().path(created.getKey()).build();
   return Response.created(location).header(RESTHeaders.RESOURCE_KEY, created.getKey()).build();
 }