@Transactional
  public Item save(Item entity) {
    checkItemKeyLength(entity.getKey());
    checkItemType(entity.getType());
    checkItemValueLength(entity.getNamespaceId(), entity.getValue());

    entity.setId(0);//protection

    if (entity.getLineNum() == 0) {
      Item lastItem = findLastOne(entity.getNamespaceId());
      int lineNum = lastItem == null ? 1 : lastItem.getLineNum() + 1;
      entity.setLineNum(lineNum);
    }

    Item item = itemRepository.save(entity);

    auditService.audit(Item.class.getSimpleName(), item.getId(), Audit.OP.INSERT,
                       item.getDataChangeCreatedBy());

    return item;
  }
@Test
    @Sql(scripts = "/sql/item-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
    @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
    public void testSaveItem() {
        Item item = new Item();
        item.setNamespaceId(3);
        item.setKey("k3");
        item.setType(-1);
        item.setValue("v3");
        item.setComment("");
        item.setLineNum(3);

        try {
            itemService.save(item);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof BadRequestException);
        }

        item.setType(0);
        Item dbItem = itemService.save(item);
        Assert.assertEquals(0, dbItem.getType());
    }