Example usage for org.springframework.data.jpa.repository JpaRepository findOne

List of usage examples for org.springframework.data.jpa.repository JpaRepository findOne

Introduction

In this page you can find the example usage for org.springframework.data.jpa.repository JpaRepository findOne.

Prototype

<S extends T> Optional<S> findOne(Example<S> example);

Source Link

Document

Returns a single entity matching the given Example or null if none was found.

Usage

From source file:com.nestedbird.modules.formparser.FormParse.java

/**
 * Parse Database Index/*  w w  w  .j a v a 2  s  .c o m*/
 * Retrieves the object from the database so we can save the object to the entity
 *
 * @param value           entity id
 * @param repositoryClass repository of entity
 * @return the entity
 */
private Object parseDatabaseIndex(final String value, final Class repositoryClass) {
    final JpaRepository repository = getFieldRepository(repositoryClass);
    return repository.findOne(value);
}

From source file:com.nestedbird.modules.formparser.FormParse.java

/**
 * Updates a BaseEntity in the database//from w  w w . j  a va  2 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);
}