Package org.eclipse.rdf4j.spring.dao

## Class RDF4JCRUDDao<ENTITY,​INPUT,​ID>

- java.lang.Object
- - org.eclipse.rdf4j.spring.dao.RDF4JDao
  - - org.eclipse.rdf4j.spring.dao.RDF4JCRUDDao<ENTITY,​INPUT,​ID>

- Type Parameters:
    `ENTITY` -
    `INPUT` -
    `ID` -
  Direct Known Subclasses:
    `SimpleRDF4JCRUDDao`

      public abstract class RDF4JCRUDDao<ENTITY,​INPUT,​ID>
      extends RDF4JDao

  Base class for DAOs providing CRUD functionality. The class allows for entities to be represented with different classes for read (ENTITY type) vs write (INPUT type) operations. DAOs that do not require this distinction must use the same class for both parameters.
  Since:
    4.0.0
  Author:
    Florian Kleedorfer

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.eclipse.rdf4j.spring.dao.RDF4JDao

      `RDF4JDao.NamedSparqlSupplierFinishBuilder, RDF4JDao.NamedSparqlSupplierPreparer`

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static String` | `KEY_PREFIX_INSERT` |
    `static String` | `KEY_PREFIX_UPDATE` |

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate)` | Constructor to be used by implementations that use IRI for the ID type.
    `RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate, Class<ID> idClass)` | Constructor that provides the type of the ID to the base implementation.

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected IRI` | `convertIdToIri​(ID id)` | Converts the provided id to an IRI.
    `void` | `delete​(ID id)` | Naive implementation using `RDF4JTemplate.delete(IRI)`.
    `protected void` | `deleteForUpdate​(ID id)` | When updating an entity via `save(Object)`, its triples are removed first using this method.
    `protected ID` | `generateNewId​(ID providedId)` | Generates a new id for an entity.
    `ENTITY` | `getById​(ID id)` | Obtains the entity with the specified id, throwing an exception if none is found.
    `Optional<ENTITY>` | `getByIdOptional​(ID id)` | Obtains an optional entity with the specified id.
    `protected ID` | `getInputId​(INPUT input)` | Obtains the id of the input instance or null if it is new (or a partially populated composite key).
    `protected NamedSparqlSupplier` | `getInsertSparql​(INPUT input)` | Returns the SPARQL string used to write an instance of T to the database.
    `protected String` | `getReadQuery()` | Returns the SPARQL string used to read an instance of T from the database.
    `protected NamedSparqlSupplier` | `getUpdateSparql​(INPUT input)` | Returns the SPARQL string used to update an instance of T in the database.
    `List<ENTITY>` | `list()` |
    `protected ENTITY` | `mapSolution​(BindingSet querySolution)` | Map one solution of the readQuery to the type of this DAO.
    `protected static BindingsBuilder` | `newBindingsBuilder()` | Returns a new BindingsBuilder for your convenience.
    `protected void` | `populateBindingsForUpdate​(MutableBindings bindingsBuilder, INPUT input)` | Sets the non-id bindings on for the write query such that the instance of type I is written to the database.
    `protected abstract void` | `populateIdBindings​(MutableBindings bindingsBuilder, ID id)` | Binds the instance id to query variable(s).
    `protected ENTITY` | `postProcessMappedSolution​(ENTITY entity)` | Callback invoked after mapping a solution to an entity, allowing subclasses to modify the entity before returning it to the client.
    `protected void` | `postProcessUpdate​(INPUT input, Map<String,​Value> bindings)` | Callback invoked after a successful insert/update.
    `ENTITY` | `save​(INPUT input)` | Saves the entity, loads it again and returns it.
    `ID` | `saveAndReturnId​(INPUT input)` |
    `ID` | `saveAndReturnId​(INPUT input, ID id)` | Saves the entity and returns its (possibly newly generated) ID.

    - ### Methods inherited from class org.eclipse.rdf4j.spring.dao.RDF4JDao

      `getNamedGraphQuery, getNamedSparqlString, getNamedSparqlSupplier, getNamedTupleQuery, getNamedUpdate, getRdf4JTemplate, prepareNamedSparqlSuppliers`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### KEY_PREFIX_INSERT

          public static final String KEY_PREFIX_INSERT

      See Also:
        Constant Field Values

    - #### KEY_PREFIX_UPDATE

          public static final String KEY_PREFIX_UPDATE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### RDF4JCRUDDao

          public RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate,
                              Class<ID> idClass)

      Constructor that provides the type of the ID to the base implementation. This constructor has to be used if the ID is anything but IRI.

    - #### RDF4JCRUDDao

          public RDF4JCRUDDao​(RDF4JTemplate rdf4JTemplate)

      Constructor to be used by implementations that use IRI for the ID type.

  - ### Method Detail

    - #### save

          public final ENTITY save​(INPUT input)

      Saves the entity, loads it again and returns it. If the modified entity is not required, clients should prefer `saveAndReturnId(Object, Object)` or `saveAndReturnId(Object)`

    - #### saveAndReturnId

          public ID saveAndReturnId​(INPUT input)

    - #### saveAndReturnId

          public ID saveAndReturnId​(INPUT input,
                                    ID id)

      Saves the entity and returns its (possibly newly generated) ID.
      Parameters:
        `input` - the entity
        `id` - the id or null for a new entity.
      Returns:
        the id (a newly generated one if the specified `id` is null, otherwise just `id`.

    - #### deleteForUpdate

          protected void deleteForUpdate​(ID id)

      When updating an entity via `save(Object)`, its triples are removed first using this method. The default implementation used `RDF4JTemplate.deleteTriplesWithSubject(IRI)`. If more complex deletion behaviour (e.g. cascading) is needed, this method should be overriden.

    - #### convertIdToIri

          protected IRI convertIdToIri​(ID id)

      Converts the provided id to an IRI. The default implementation only works for DAOs that use IRI ids.
      Parameters:
        `id` -
      Returns:

    - #### generateNewId

          protected ID generateNewId​(ID providedId)

      Generates a new id for an entity. The default implementation only works for IRI ids.
      Parameters:
        `providedId` -
      Returns:
        a new id.

    - #### list

          public final List<ENTITY> list()

    - #### getById

          public final ENTITY getById​(ID id)

      Obtains the entity with the specified id, throwing an exception if none is found.
      Parameters:
        `id` - the id
      Returns:
        the entity
      Throws:
        `IncorrectResultSetSizeException` - if no entity is found with the specified id

    - #### getByIdOptional

          public final Optional<ENTITY> getByIdOptional​(ID id)

      Obtains an optional entity with the specified id.
      Parameters:
        `id` - the id
      Returns:
        an Optional maybe containing the entity

    - #### delete

          public void delete​(ID id)

      Naive implementation using `RDF4JTemplate.delete(IRI)`. DAOs that need more complex deletion behaviour (e.g. cascading) should override this method.

    - #### getReadQuery

          protected String getReadQuery()

      Returns the SPARQL string used to read an instance of T from the database. The base implementation will cache the query string, so implementations should not try to cache the query.

    - #### mapSolution

          protected ENTITY mapSolution​(BindingSet querySolution)

      Map one solution of the readQuery to the type of this DAO.

    - #### postProcessMappedSolution

          protected ENTITY postProcessMappedSolution​(ENTITY entity)

      Callback invoked after mapping a solution to an entity, allowing subclasses to modify the entity before returning it to the client.

    - #### postProcessUpdate

          protected void postProcessUpdate​(INPUT input,
                                           Map<String,​Value> bindings)

      Callback invoked after a successful insert/update.

    - #### getInsertSparql

          protected NamedSparqlSupplier getInsertSparql​(INPUT input)

      Returns the SPARQL string used to write an instance of T to the database. The instance to be inserted is passed to the function so implementations can decide which query to use based on the instance.

    - #### getUpdateSparql

          protected NamedSparqlSupplier getUpdateSparql​(INPUT input)

      Returns the SPARQL string used to update an instance of T in the database. The instance to be updated is passed to the function so implementations can decide which query to use based on the instance.

    - #### populateIdBindings

          protected abstract void populateIdBindings​(MutableBindings bindingsBuilder,
                                                     ID id)

      Binds the instance id to query variable(s).

    - #### populateBindingsForUpdate

          protected void populateBindingsForUpdate​(MutableBindings bindingsBuilder,
                                                   INPUT input)

      Sets the non-id bindings on for the write query such that the instance of type I is written to the database. ID bindings are set through populateIdBindings()

    - #### getInputId

          protected ID getInputId​(INPUT input)

      Obtains the id of the input instance or null if it is new (or a partially populated composite key).

    - #### newBindingsBuilder

          protected static BindingsBuilder newBindingsBuilder()

      Returns a new BindingsBuilder for your convenience.