StorageJetCache extends SimpleJpaRepository<E,PK> implements IStorage<PK,E> { @Override @Transactional(rollbackFor = Exception.class) public E create(E entity) { Preconditions.checkNotNull(entity,"entity不能为null"); CacheGetResult<E> cacheGetResult = cache.GET(entity.getId()); if (cacheGetResult.isSuccess()){ E entityFromCache = cacheGetResult.getValue(); if (entityFromCache == null) { return doInsert(entity); } else { throw new EntityExistsException("数据库中已存在该实体，考虑使用update?"); } } else { if (exists(entity.getId())){ throw new EntityExistsException("数据库中已存在该实体，考虑使用update?"); }else { return doInsert(entity); } } } StorageJetCache(JpaEntityInformation<E, ?> entityInformation, EntityManager entityManager); @Override @Transactional(rollbackFor = Exception.class) E create(E entity); @Override @Transactional(rollbackFor = Exception.class) void update(E entity); @Override void mergeUpdate(E entity); @Override E get(PK id); @Override @Transactional(rollbackFor = Exception.class) E getOrCreate(PK id, EntityCreator<PK, E> entityCreator); @Override @Transactional(rollbackFor = Exception.class) E remove(PK id); void setDelay(int delay); void setProxy(IStorage<PK, E> proxy); void setCache(Cache<PK, E> cache);  }
@Test void create() throws Exception { String account = "sandoTestCreate"; AccountEntity accountEntity = new AccountEntity(); accountEntity.setAccount(account); accountEntity.setPassword("123556"); Assertions.assertThrows(DataIntegrityViolationException.class,()->{ iStorage.create(accountEntity); }); System.in.read(); }