List of usage examples for org.springframework.data.jpa.repository JpaRepository saveAndFlush
<S extends T> S saveAndFlush(S entity);
From source file:com.nestedbird.modules.formparser.FormParse.java
/** * Updates a BaseEntity in the database//from w w w .j a v a2 s.c o m * * @param value data to edit * @param fieldType type of baseentity * @return edited base entity * @throws JSONException the exception */ private BaseEntity parseBaseEntity(final JSONObject value, final Class fieldType) throws JSONException { final JpaRepository repository = getFieldRepository(fieldType); Optional<BaseEntity> entity = Optional.empty(); if (value.has("id") && (!value.isNull("id"))) { final String id = value.get("id").toString(); entity = Optional.ofNullable((BaseEntity) repository.findOne(id)); } if (!entity.isPresent()) { try { entity = Optional.ofNullable((BaseEntity) Class.forName(fieldType.getName()).newInstance()); } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e1) { logger.info("[FormParse] [parseBaseEntity] Failure To Create Class Instance", e1); } } entity.ifPresent(e -> { final ParameterMapParser parser = new ParameterMapParser(value); parser.loopData((key, data) -> writeToEntity(e, key, data)); repository.saveAndFlush(e); }); return entity.orElseGet(null); }
From source file:org.duracloud.account.db.util.DbUtil.java
private void saveEntities(List<BaseEntity> entities) { if (!entities.isEmpty()) { final JpaRepository repo = getRepo(entities.get(0)); for (final BaseEntity entity : entities) { // Entities with relationship need to have their related // entities looked up and then set to save properly. if (entity instanceof AccountInfo) { AccountInfo sd = (AccountInfo) entity; sd.setPrimaryStorageProviderAccount(repoMgr.getStorageProviderAccountRepo() .findOne(sd.getPrimaryStorageProviderAccount().getId())); Set<StorageProviderAccount> storageProviderAccounts = sd.getSecondaryStorageProviderAccounts(); try { if (storageProviderAccounts.size() > 0) { Set<StorageProviderAccount> accounts = new HashSet<>(); for (StorageProviderAccount sp : storageProviderAccounts) { StorageProviderAccount account = repoMgr.getStorageProviderAccountRepo() .findOne(sp.getId()); accounts.add(account); }//from www .j a v a 2 s .co m sd.setSecondaryStorageProviderAccounts(accounts); log.warn("Set a secondary storage provider to ServerDetails with id " + sd.getId()); } } catch (LazyInitializationException e) { // do nothing, there's no secondary storage providers // for this ServerDetails entity } catch (Exception e) { log.warn("Exception not handled!!!"); log.warn(e.toString()); } repo.saveAndFlush(sd); } else if (entity instanceof DuracloudGroup) { DuracloudGroup dg = (DuracloudGroup) entity; dg.setAccount(repoMgr.getAccountRepo().findOne(dg.getAccount().getId())); if (dg.getUsers().size() > 0) { Set<DuracloudUser> users = new HashSet<>(); for (DuracloudUser user : dg.getUsers()) { users.add(repoMgr.getUserRepo().findOne(user.getId())); } dg.setUsers(users); } repo.saveAndFlush(dg); } else if (entity instanceof AccountRights) { AccountRights rights = (AccountRights) entity; rights.setAccount(repoMgr.getAccountRepo().findOne(rights.getAccount().getId())); rights.setUser(repoMgr.getUserRepo().findOne(rights.getUser().getId())); repo.saveAndFlush(rights); } else if (entity instanceof UserInvitation) { UserInvitation ui = (UserInvitation) entity; ui.setAccount(repoMgr.getAccountRepo().findOne(ui.getAccount().getId())); repo.saveAndFlush(ui); } else { repo.saveAndFlush(entity); } } } }