Class CrudRepositoryWriter

java.lang.Object
com.reallifedeveloper.tools.test.database.CrudRepositoryWriter

public class CrudRepositoryWriter extends Object
A helper class to write data into a CrudRepository from some data source, e.g., a CSV file, where each entity is represented by a CrudRepositoryWriter.DbTableRow.

This can be useful for inserting test data into a repository, irrespective of whether the repository connects to a real database or not.

TODO: The current implementation only has basic support for "to many" associations (there must be a &JoinTable annotation on a field, with &JoinColumn annotations), and for enums (an enum must be stored as a string).

Author:
RealLifeDeveloper
  • Constructor Details

    • CrudRepositoryWriter

      public CrudRepositoryWriter()
  • Method Details

    • writeEntity

      public <T, E, ID extends Serializable> boolean writeEntity(CrudRepositoryWriter.DbTableRow tableRow, Class<T> repositoryEntityType, @Nullable Class<E> entityType, org.springframework.data.repository.CrudRepository<T,ID> repository, String tableName) throws ReflectiveOperationException
      Creates a new entity based on data from a CrudRepositoryWriter.DbTableRow and writes it into a repository if appropriate.

      This method may create entities that are not directly handled by the repository, in which case they are assumed to be related to some entity in the repository.

      Type Parameters:
      T - the type of entities in the repository
      E - the type of entity being created
      ID - the type of the primary key of the entities in the repository
      Parameters:
      tableRow - the TableRow with the data to insert into the fields of the newly created entity
      repositoryEntityType - the class object representing T, i.e., the type ofrepository entities
      entityType - the class object representing E, i.e., the type of entity being created, or null
      repository - the repository in which to insert the newly created entity
      tableName - the name of the database table where the entity should be stored
      Returns:
      true if an entity was created, no matter if it was saved in the repository, false otherwise
      Throws:
      ReflectiveOperationException - if some reflection operation failed creating the entity or setting is fields
    • addEntitiesFromJoinTable

      public void addEntitiesFromJoinTable(CrudRepositoryWriter.DbTableRow tableRow, String joinTtableName)
      Connects entities based on data in a join table.
      Parameters:
      tableRow - the TableRow with the data for the join table
      joinTtableName - the name of the join table to use to connect entities
    • fillReferencesBetweenEntities

      public void fillReferencesBetweenEntities() throws ReflectiveOperationException
      Goes through all entities that have been saved, trying to fix missing associations.
      Throws:
      ReflectiveOperationException - if something went wrong using reflection to analyze the entities