StorageJetCache extends SimpleJpaRepository<E,PK> implements IStorage<PK,E> { @Override @Transactional(rollbackFor = Exception.class) public E getOrCreate(PK id, EntityCreator<PK, E> entityCreator) { Preconditions.checkNotNull(id,"id不能为null"); E e = get(id); if (e!=null){ return e; }else { E entityCreated = entityCreator.create(id); return doInsert(entityCreated); } } 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 getOrCreate() { String account = "getOrCreate"; iStorage.remove(account); AccountEntity accountEntity = iStorage.getOrCreate(account, (id) -> { AccountEntity playerAccountEntity = new AccountEntity(); playerAccountEntity.setAccount(id); playerAccountEntity.setPassword("123556"); return playerAccountEntity; }); Assertions.assertEquals(iStorage.get(account),accountEntity); }