Class CrudAggregateRepository<T,K extends Serializable>
- java.lang.Object
-
- com.totvs.tjf.repository.aggregate.CrudAggregateRepository<T,K>
-
- Type Parameters:
T-@Aggregateclass.K-@AggregateIdentifierclass.
- All Implemented Interfaces:
AggregateRepository<T,K>
public abstract class CrudAggregateRepository<T,K extends Serializable> extends Object implements AggregateRepository<T,K>
Repository implementation to store@Aggregateas documents in databases with JSON support.- Author:
- Thiago A. de Souza Weber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCrudAggregateRepository.SqlTypes
-
Constructor Summary
Constructors Modifier Constructor Description protectedCrudAggregateRepository(javax.persistence.EntityManager em, com.fasterxml.jackson.databind.ObjectMapper mapper)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidclose(ResultSet resultSet)Safe close theResultSet.protected com.fasterxml.jackson.databind.ObjectMapperconfigureObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)Configure theObjectMapperfor serialize and deserialize the@Aggregate.protected org.springframework.jdbc.core.SqlParameterValue[]convertObjectToSqlParameterValue(Object... arguments)Longcount(String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns total registers of a table, matching the given clause and arguments or 0 if none found.voiddelete(K identifier)Delete the@Aggregatewith the given identifier.protected Tdeserialize(byte[] stream)Deserialize the given data stream (JSON string) to an@Aggregateinstance.booleanexists(String clause, Object... arguments)Checks if, at least one,@Aggregateexists matching the given clause and arguments.booleanexists(String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)Checks if, at least one,@Aggregateexists matching the given clause and arguments.booleanexists(K identifier)Checks if the@Aggregateexists with the given identifier.com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T>findAll(String clause, String orderby, Integer limit, Integer offset, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.Collection<T>findAll(String clause, String orderby, Object... arguments)Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.Collection<T>findAll(String clause, String orderby, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.Collection<T>findAll(String clause, javax.persistence.LockModeType lockModeType, String orderby, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.Optional<T>findOne(String clause, Object... arguments)Returns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.Optional<T>findOne(String clause, javax.persistence.LockModeType lockModeType, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.Optional<T>findOne(String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.Optional<T>get(K identifier)Returns a single@Aggregate, matching the given identifier orOptional.empty()if none found.protected Optional<T>getAggregate(Connection connection, String clause, javax.persistence.LockModeType lockModeType, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.protected ObjectgetAggregateAsDatabaseParameter(String dbName, String json)Build the correct database parameter type to store the serialized@Aggregateinstance based on the database name.protected AggregateMetadata<T,K>getAggregateMetadata()Retrieves theAggregateMetadatafrom theAggregateRepositoryimplementation.protected com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T>getAggregates(Connection connection, String clause, javax.persistence.LockModeType lockModeType, String orderby, Integer limit, Integer offset, org.springframework.jdbc.core.SqlParameterValue... arguments)Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.protected longgetCount(Connection connection, String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)protected byte[]getDataFromResultSet(String dbName, ResultSet result)Read theResultSetdata parameter based on the correct database type.Optional<AggregateMetadataInfoContent>getMetadata(K identifier)Returns the aggregateAggregateMetadataInfoContent, matching the given identifier orOptional.empty()if none found.protected com.fasterxml.jackson.databind.ObjectMappergetObjectMapper()Retrieves theObjectMapperto be used to serialize and deserialize the@Aggregate.org.springframework.context.ApplicationEventPublishergetPublisher()protected org.hibernate.SessiongetSession()Retrieve the currentSessionfrom theEntityManager.protected StringgetTableName()Deprecated, for removal: This API element is subject to removal in a future version.in favor ofTableannotationTinsert(T aggregate)Serialize the given@Aggregatestate and store at the database as a document.protected booleanisShowSqlEnabled()Checks if show SQL is enabled.protected Stringserialize(T aggregate)Serialize the given@Aggregateto a json.voidsetPublisher(org.springframework.context.ApplicationEventPublisher publisher)protected PreparedStatementsetStatementParameters(PreparedStatement statement, org.springframework.jdbc.core.SqlParameterValue... arguments)Add all the parameters to thePreparedStatement.Tupdate(T aggregate)Serialize the given@Aggregatestate and update document in the database .-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.totvs.tjf.repository.aggregate.AggregateRepository
findAll
-
-
-
-
Method Detail
-
getTableName
@Deprecated(since="2.0.0-RELEASE", forRemoval=true) protected String getTableName()
Deprecated, for removal: This API element is subject to removal in a future version.in favor ofTableannotationChecks if show SQL is enabled.- Returns:
- the
@Aggregatetable name
-
isShowSqlEnabled
protected boolean isShowSqlEnabled()
Checks if show SQL is enabled.- Returns:
- true if show SQL is enabled, otherwise false.
-
getSession
protected org.hibernate.Session getSession()
Retrieve the currentSessionfrom theEntityManager.- Returns:
- the current
Session. - See Also:
EntityManager.unwrap(Class),Session
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
Retrieves theObjectMapperto be used to serialize and deserialize the@Aggregate.- Returns:
- the
ObjectMapper
-
getAggregateMetadata
protected AggregateMetadata<T,K> getAggregateMetadata()
Retrieves theAggregateMetadatafrom theAggregateRepositoryimplementation.- Returns:
- the
@Aggregatemetadata.
-
configureObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper configureObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
Configure theObjectMapperfor serialize and deserialize the@Aggregate.
The standard configuration stays as:- Only non nulls (see
JsonInclude.Include.NON_NULLfor more information). - Default typing specified by
AggregateTypeResolverBuilder. - Write BigDecimal as plain (see
JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAINfor more information). - Disable date as timestamps (see
SerializationFeature.WRITE_DATES_AS_TIMESTAMPSfor more information). - Disable the time zone adjustment when deserialize dates (see
DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONEfor more information). - Serialize only fields (see
PropertyAccessor.FIELDfor more information).
- Parameters:
mapper- to be configured- Returns:
- the configured
ObjectMapper
- Only non nulls (see
-
serialize
protected String serialize(@Nullable T aggregate) throws com.fasterxml.jackson.core.JsonProcessingException
Serialize the given@Aggregateto a json.- Parameters:
aggregate- instance to be serialized- Returns:
@Aggregateas json string.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- See Also:
ObjectMapper.writeValueAsString(Object)
-
deserialize
protected T deserialize(byte[] stream) throws IOException
Deserialize the given data stream (JSON string) to an@Aggregateinstance.- Parameters:
stream- of JSON string- Returns:
- an
@Aggregateinstance. - Throws:
IOException- See Also:
ObjectMapper.readValue(com.fasterxml.jackson.core.JsonParser, java.lang.Class<T>)
-
setStatementParameters
protected PreparedStatement setStatementParameters(PreparedStatement statement, @Nullable org.springframework.jdbc.core.SqlParameterValue... arguments) throws SQLException
Add all the parameters to thePreparedStatement.- Parameters:
statement- thePreparedStatement.arguments- the arguments to be added to thePreparedStatement, can be null.- Returns:
- the
PreparedStatementitself. - Throws:
SQLException- See Also:
PreparedStatement.setObject(int, Object)
-
getAggregateAsDatabaseParameter
protected Object getAggregateAsDatabaseParameter(String dbName, String json) throws SQLException
Build the correct database parameter type to store the serialized@Aggregateinstance based on the database name.- Parameters:
dbName- the database name.json- the serialized@Aggregateinstance.- Returns:
- the corrected parameter type to database.
- Throws:
SQLException
-
getDataFromResultSet
protected byte[] getDataFromResultSet(String dbName, ResultSet result) throws SQLException
Read theResultSetdata parameter based on the correct database type.- Parameters:
dbName- the database name.result- theResultSetfrom the query.- Returns:
- the data to be deserialized.
- Throws:
SQLException
-
exists
@Transactional(readOnly=true) public boolean exists(K identifier)
Description copied from interface:AggregateRepositoryChecks if the@Aggregateexists with the given identifier.- Specified by:
existsin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
identifier- can't be null.- Returns:
trueif exists, otherwise false.
-
exists
@Transactional(readOnly=true) public boolean exists(String clause, Object... arguments)
Description copied from interface:AggregateRepositoryChecks if, at least one,@Aggregateexists matching the given clause and arguments.- Specified by:
existsin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
trueif found at least one, otherwise false.
-
exists
@Transactional(readOnly=true) public boolean exists(@Nullable String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)Description copied from interface:AggregateRepositoryChecks if, at least one,@Aggregateexists matching the given clause and arguments.- Specified by:
existsin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
trueif found at least one, otherwise false.
-
get
@Transactional(readOnly=true) public Optional<T> get(K identifier)
Description copied from interface:AggregateRepositoryReturns a single@Aggregate, matching the given identifier orOptional.empty()if none found.- Specified by:
getin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
identifier- can't be null.- Returns:
- an
Optionalinstance with the@Aggregateif found, otherwiseOptional.empty().
-
getMetadata
@Transactional(readOnly=true) public Optional<AggregateMetadataInfoContent> getMetadata(K identifier)
Description copied from interface:AggregateRepositoryReturns the aggregateAggregateMetadataInfoContent, matching the given identifier orOptional.empty()if none found.- Specified by:
getMetadatain interfaceAggregateRepository<T,K extends Serializable>- Parameters:
identifier- can't be null.- Returns:
- an
Optionalinstance with the aggregateAggregateMetadataInfoContentif found, otherwiseOptional.empty().
-
findOne
@Transactional(readOnly=true) public Optional<T> findOne(String clause, Object... arguments)
Description copied from interface:AggregateRepositoryReturns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.- Specified by:
findOnein interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- an
Optionalinstance with the@Aggregateif found any, otherwiseOptional.empty().
-
findOne
@Transactional(readOnly=true) public Optional<T> findOne(@Nullable String clause, org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.- Specified by:
findOnein interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- an
Optionalinstance with the@Aggregateif found any, otherwiseOptional.empty().
-
findOne
public Optional<T> findOne(@Nullable String clause, javax.persistence.LockModeType lockModeType, org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.- Specified by:
findOnein interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.lockModeType- pass lock mode type if necessary.arguments- the arguments to be used for lookup, can be null.- Returns:
- an
Optionalinstance with the@Aggregateif found any, otherwiseOptional.empty().
-
findAll
@Transactional(readOnly=true) public Collection<T> findAll(String clause, @Nullable String orderby, Object... arguments)
Description copied from interface:AggregateRepositoryReturns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.- Specified by:
findAllin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.orderby- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- a
Collectioninstance with all the@Aggregatefound, otherwiseCollections.emptyList().
-
findAll
@Transactional(readOnly=true) public Collection<T> findAll(@Nullable String clause, @Nullable String orderby, org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.- Specified by:
findAllin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.orderby- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- a
Collectioninstance with all the@Aggregatefound, otherwiseCollections.emptyList().
-
findAll
@Transactional(readOnly=true) public com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T> findAll(@Nullable String clause, @Nullable String orderby, @Nullable Integer limit, @Nullable Integer offset, org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.- Specified by:
findAllin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.orderby- can be null.limit- can be null.offset- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- a
AggregateCollectionResultinstance with all the@Aggregatefound, otherwise itemsCollections.emptyList().
-
findAll
public Collection<T> findAll(@Nullable String clause, javax.persistence.LockModeType lockModeType, @Nullable String orderby, org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.- Specified by:
findAllin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.lockModeType- pass lock mode type if necessary.orderby- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- a
Collectioninstance with all the@Aggregatefound, otherwiseCollections.emptyList().
-
count
public Long count(@Nullable String clause, @Nullable org.springframework.jdbc.core.SqlParameterValue... arguments)
Description copied from interface:AggregateRepositoryReturns total registers of a table, matching the given clause and arguments or 0 if none found.- Specified by:
countin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
clause- can be null.arguments- the arguments to be used for lookup, can be null.- Returns:
- total registers of a table if found any, otherwise 0.
-
getAggregate
protected Optional<T> getAggregate(Connection connection, String clause, javax.persistence.LockModeType lockModeType, org.springframework.jdbc.core.SqlParameterValue... arguments) throws SQLException
Returns a single@Aggregate, matching the given clause and arguments orOptional.empty()if none found.- Parameters:
connection- connectionclause- can be null.lockModeType- pass lock mode type, can be PESSIMISTIC_READ or NONE.arguments- the arguments to be used for lookup, can be null.- Returns:
- an
Optionalinstance with the@Aggregateif found any, otherwiseOptional.empty(). - Throws:
SQLException
-
getAggregates
protected com.totvs.tjf.core.aggregate.repository.AggregateCollectionResult<T> getAggregates(Connection connection, String clause, @Nullable javax.persistence.LockModeType lockModeType, @Nullable String orderby, Integer limit, Integer offset, org.springframework.jdbc.core.SqlParameterValue... arguments) throws SQLException
Returns all@Aggregates, matching the given clause and arguments orCollections.emptyList()if none found.- Parameters:
connection- connectionclause- can be null.lockModeType- pass lock mode type if necessary, can be PESSIMISTIC_READ or NONE.orderby- pass order by definition if necessary.limit- limit of query rowsoffset- result starting at rowarguments- the arguments to be used for lookup, can be null.- Returns:
Collectioninstance with all the@Aggregatefound, otherwiseCollections.emptyList().- Throws:
SQLException
-
insert
@Transactional public T insert(T aggregate)
Description copied from interface:AggregateRepositorySerialize the given@Aggregatestate and store at the database as a document.- Specified by:
insertin interfaceAggregateRepository<T,K extends Serializable>- Parameters:
aggregate- instance to be created- Returns:
- the saved aggregate
-
update
@Transactional public T update(T aggregate)
Description copied from interface:AggregateRepositorySerialize the given@Aggregatestate and update document in the database .- Specified by:
updatein interfaceAggregateRepository<T,K extends Serializable>- Parameters:
aggregate- instance to be store- Returns:
- the modified aggregate
-
delete
public void delete(K identifier)
Description copied from interface:AggregateRepositoryDelete the@Aggregatewith the given identifier.- Specified by:
deletein interfaceAggregateRepository<T,K extends Serializable>- Parameters:
identifier- of theAggregate
-
getCount
protected long getCount(Connection connection, @Nullable String clause, @Nullable org.springframework.jdbc.core.SqlParameterValue... arguments) throws SQLException
- Throws:
SQLException
-
close
protected void close(@Nullable ResultSet resultSet)Safe close theResultSet.- Parameters:
resultSet- current, open,ResultSet.- See Also:
ResultSet.close()
-
convertObjectToSqlParameterValue
protected org.springframework.jdbc.core.SqlParameterValue[] convertObjectToSqlParameterValue(Object... arguments)
-
getPublisher
public org.springframework.context.ApplicationEventPublisher getPublisher()
-
setPublisher
public void setPublisher(org.springframework.context.ApplicationEventPublisher publisher)
-
-