Class CrudRepositoryWriter
java.lang.Object
com.reallifedeveloper.tools.test.database.CrudRepositoryWriter
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents the value of a single field in the database.static interfaceRepresents one row of data from the database. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEntitiesFromJoinTable(CrudRepositoryWriter.DbTableRow tableRow, String joinTtableName) Connects entities based on data in a join table.voidGoes through all entities that have been saved, trying to fix missing associations.<T,E, ID extends Serializable>
booleanwriteEntity(CrudRepositoryWriter.DbTableRow tableRow, Class<T> repositoryEntityType, @Nullable Class<E> entityType, org.springframework.data.repository.CrudRepository<T, ID> repository, String tableName) Creates a new entity based on data from aCrudRepositoryWriter.DbTableRowand writes it into a repository if appropriate.
-
Constructor Details
-
CrudRepositoryWriter
public CrudRepositoryWriter()
-
-
Method Details
-
writeEntity
public <T,E, boolean writeEntityID extends Serializable> (CrudRepositoryWriter.DbTableRow tableRow, Class<T> repositoryEntityType, @Nullable Class<E> entityType, org.springframework.data.repository.CrudRepository<T, ID> repository, String tableName) throws ReflectiveOperationExceptionCreates a new entity based on data from aCrudRepositoryWriter.DbTableRowand 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 repositoryE- the type of entity being createdID- the type of the primary key of the entities in the repository- Parameters:
tableRow- theTableRowwith the data to insert into the fields of the newly created entityrepositoryEntityType- the class object representingT, i.e., the type ofrepository entitiesentityType- the class object representingE, i.e., the type of entity being created, ornullrepository- the repository in which to insert the newly created entitytableName- the name of the database table where the entity should be stored- Returns:
trueif an entity was created, no matter if it was saved in the repository,falseotherwise- 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- theTableRowwith the data for the join tablejoinTtableName- the name of the join table to use to connect entities
-
fillReferencesBetweenEntities
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
-