Search in sources :

Example 1 with FieldMapping

use of org.eclipse.jnosql.mapping.reflection.FieldMapping in project jnosql-diana by eclipse.

the class AbstractDocumentTemplate method delete.

@Override
public <T, K> void delete(Class<T> entityClass, K id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassMapping classMapping = getClassMappings().get(entityClass);
    FieldMapping idField = classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classMapping, idField.getFieldName(), getConverters());
    DocumentDeleteQuery query = DocumentDeleteQuery.delete().from(classMapping.getName()).where(idField.getName()).eq(value).build();
    delete(query);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery)

Example 2 with FieldMapping

use of org.eclipse.jnosql.mapping.reflection.FieldMapping in project jnosql-diana by eclipse.

the class AbstractColumnTemplate method delete.

@Override
public <T, K> void delete(Class<T> entityClass, K id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassMapping classMapping = getClassMappings().get(entityClass);
    FieldMapping idField = classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classMapping, idField.getFieldName(), getConverters());
    ColumnDeleteQuery query = ColumnDeleteQuery.delete().from(classMapping.getName()).where(idField.getName()).eq(value).build();
    getManager().delete(query);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) ColumnDeleteQuery(jakarta.nosql.column.ColumnDeleteQuery)

Example 3 with FieldMapping

use of org.eclipse.jnosql.mapping.reflection.FieldMapping in project jnosql-diana by eclipse.

the class AbstractGraphConverter method feedId.

private <T> void feedId(Vertex vertex, T entity) {
    ClassMapping mapping = getClassMappings().get(entity.getClass());
    Optional<FieldMapping> id = mapping.getId();
    Object vertexId = vertex.id();
    if (Objects.nonNull(vertexId) && id.isPresent()) {
        FieldMapping fieldMapping = id.get();
        if (fieldMapping.getConverter().isPresent()) {
            AttributeConverter attributeConverter = getConverters().get(fieldMapping.getConverter().get());
            Object attributeConverted = attributeConverter.convertToEntityAttribute(vertexId);
            fieldMapping.write(entity, fieldMapping.getValue(Value.of(attributeConverted)));
        } else {
            fieldMapping.write(entity, fieldMapping.getValue(Value.of(vertexId)));
        }
    }
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) AttributeConverter(jakarta.nosql.mapping.AttributeConverter) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping)

Example 4 with FieldMapping

use of org.eclipse.jnosql.mapping.reflection.FieldMapping in project jnosql-diana by eclipse.

the class AbstractGraphTemplate method find.

@Override
public <T, K> Optional<T> find(Class<T> entityClass, K id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassMapping classMapping = getClassMappings().get(entityClass);
    FieldMapping idField = classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classMapping, idField.getFieldName(), getConverters());
    final Optional<Vertex> vertex = getTraversal().V(value).hasLabel(classMapping.getName()).tryNext();
    return (Optional<T>) vertex.map(getConverter()::toEntity);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Optional(java.util.Optional) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping)

Example 5 with FieldMapping

use of org.eclipse.jnosql.mapping.reflection.FieldMapping in project jnosql-diana by eclipse.

the class AbstractGraphTemplate method isIdNull.

private <T> boolean isIdNull(T entity) {
    ClassMapping classMapping = getClassMappings().get(entity.getClass());
    FieldMapping field = classMapping.getId().get();
    return isNull(field.read(entity));
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping)

Aggregations

FieldMapping (org.eclipse.jnosql.mapping.reflection.FieldMapping)10 ClassMapping (org.eclipse.jnosql.mapping.reflection.ClassMapping)8 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 ColumnDeleteQuery (jakarta.nosql.column.ColumnDeleteQuery)1 ColumnQuery (jakarta.nosql.column.ColumnQuery)1 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)1 DocumentQuery (jakarta.nosql.document.DocumentQuery)1 AttributeConverter (jakarta.nosql.mapping.AttributeConverter)1 Optional (java.util.Optional)1