- All Superinterfaces:
QueryTemplate,RepositoryLookup,SubqueryTemplate
ORMTemplate extends both QueryTemplate (for constructing and executing SQL queries) and
RepositoryLookup (for obtaining type-safe EntityRepository and ProjectionRepository
instances). It is the central interface from which all database operations originate.
Instances are created using the static factory methods of(javax.sql.DataSource) or
of(java.sql.Connection), or via the convenience methods in the Templates class.
For JPA-based usage, see JpaTemplate.
Example
ORMTemplate orm = ORMTemplate.of(dataSource);
// Repository-based access
EntityRepository<User, Integer> users = orm.entity(User.class);
Optional<User> user = users.findById(42);
// Template-based query
List<User> result = orm.query(RAW."""
SELECT \{User.class}
FROM \{User.class}
WHERE \{User_.name} = \{"Alice"}""")
.getResultList(User.class);
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ORMTemplateof(Connection connection) Returns anORMTemplatefor use with JDBC.static ORMTemplateof(Connection connection, UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, with a custom template decorator.static ORMTemplateof(Connection connection, StormConfig config) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfig.static ORMTemplateof(Connection connection, StormConfig config, UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfigand a custom template decorator.static ORMTemplateof(DataSource dataSource) Returns anORMTemplatefor use with JDBC.static ORMTemplateof(DataSource dataSource, UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, with a custom template decorator.static ORMTemplateof(DataSource dataSource, StormConfig config) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfig.static ORMTemplateof(DataSource dataSource, StormConfig config, UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfigand a custom template decorator.Validates all discovered entity and projection types against the database schema.validateSchema(Iterable<Class<? extends Data>> types) Validates the specified types against the database schema.validateSchema(Predicate<Class<? extends Data>> filter) Validates discovered types matching the filter against the database schema.voidValidates all discovered types and throws if any errors are found.voidvalidateSchemaOrThrow(Iterable<Class<? extends Data>> types) Validates the specified types and throws if any errors are found.voidvalidateSchemaOrThrow(Predicate<Class<? extends Data>> filter) Validates discovered types matching the filter and throws if any errors are found.withEntityCallback(EntityCallback<?> callback) Returns a newORMTemplatewith the specified entity callback added.withEntityCallbacks(List<EntityCallback<?>> callbacks) Returns a newORMTemplatewith the specified entity callbacks added.Methods inherited from interface st.orm.template.QueryTemplate
createBindVars, deleteFrom, dialect, model, model, query, query, ref, ref, selectFrom, selectFrom, selectFromMethods inherited from interface st.orm.repository.RepositoryLookup
entity, projection, repositoryMethods inherited from interface st.orm.template.SubqueryTemplate
subquery, subquery, subquery
-
Method Details
-
withEntityCallback
Returns a newORMTemplatewith the specified entity callback added.The returned template shares the same underlying connection and configuration, but applies the given callback to entity lifecycle operations (insert, update, delete) performed through its repositories. The callback is only invoked for entities matching its type parameter. Multiple callbacks can be registered by chaining calls to this method.
- Parameters:
callback- the entity callback to add; must not benull.- Returns:
- a new
ORMTemplatewith the callback added. - Since:
- 1.9
-
withEntityCallbacks
Returns a newORMTemplatewith the specified entity callbacks added.The returned template shares the same underlying connection and configuration, but applies the given callbacks to entity lifecycle operations (insert, update, delete) performed through its repositories. Each callback is only invoked for entities matching its type parameter.
- Parameters:
callbacks- the entity callbacks to add; must not benull.- Returns:
- a new
ORMTemplatewith the callbacks added. - Since:
- 1.9
-
validateSchema
Validates all discovered entity and projection types against the database schema.Logs each validation error and returns the list of error messages. On success, logs a confirmation message and returns an empty list.
This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Returns:
- the list of validation error messages (empty on success).
- Throws:
PersistenceException- if the template does not support schema validation.- Since:
- 1.9
-
validateSchema
Validates discovered types matching the filter against the database schema.Discovers all entity and projection types via the classpath index, applies the given filter, and validates the matching types. This is useful when a single application connects to multiple datasources and only a subset of types is reachable from each connection.
Logs each validation error and returns the list of error messages. On success, logs a confirmation message and returns an empty list.
This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Parameters:
filter- predicate to select which discovered types to validate.- Returns:
- the list of validation error messages (empty on success).
- Throws:
PersistenceException- if the template does not support schema validation.- Since:
- 1.11
-
validateSchema
Validates the specified types against the database schema.Logs each validation error and returns the list of error messages. On success, logs a confirmation message and returns an empty list.
This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Parameters:
types- the entity and projection types to validate.- Returns:
- the list of validation error messages (empty on success).
- Throws:
PersistenceException- if the template does not support schema validation.- Since:
- 1.9
-
validateSchemaOrThrow
void validateSchemaOrThrow()Validates all discovered types and throws if any errors are found.This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Throws:
PersistenceException- if validation fails or the template does not support schema validation.- Since:
- 1.9
-
validateSchemaOrThrow
Validates discovered types matching the filter and throws if any errors are found.Discovers all entity and projection types via the classpath index, applies the given filter, and validates the matching types. This is useful when a single application connects to multiple datasources and only a subset of types is reachable from each connection.
This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Parameters:
filter- predicate to select which discovered types to validate.- Throws:
PersistenceException- if validation fails or the template does not support schema validation.- Since:
- 1.11
-
validateSchemaOrThrow
Validates the specified types and throws if any errors are found.This method requires a DataSource-backed template. Templates created from a raw
ConnectionorEntityManagerdo not support schema validation.- Parameters:
types- the entity and projection types to validate.- Throws:
PersistenceException- if validation fails or the template does not support schema validation.- Since:
- 1.9
-
of
Returns anORMTemplatefor use with JDBC.This method creates an ORM repository template using the provided
DataSource. It allows you to perform database operations using JDBC in a type-safe manner.Example usage:
DataSource dataSource = ...; ORMTemplate orm = ORMTemplate.of(dataSource); List<MyTable> otherTables = orm.query(RAW.""" SELECT \{MyTable.class} FROM \{MyTable.class} WHERE \{MyTable_.name} = \{"ABC"}""") .getResultList(MyTable.class);- Parameters:
dataSource- theDataSourceto use for database operations; must not benull.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
Returns anORMTemplatefor use with JDBC.This method creates an ORM repository template using the provided
Connection. It allows you to perform database operations using JDBC in a type-safe manner.Note: The caller is responsible for closing the connection after usage.
Example usage:
try (Connection connection = ...) { ORMTemplate orm = ORMTemplate.of(connection); List<MyTable> otherTables = orm.query(RAW.""" SELECT \{MyTable.class} FROM \{MyTable.class} WHERE \{MyTable_.name} = \{"ABC"}""") .getResultList(MyTable.class) }- Parameters:
connection- theConnectionto use for database operations; must not benull.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
static ORMTemplate of(@Nonnull DataSource dataSource, @Nonnull UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, with a custom template decorator.This method creates an ORM repository template using the provided
DataSourceand applies the specified decorator to customize template processing behavior.- Parameters:
dataSource- theDataSourceto use for database operations; must not benull.decorator- a function that transforms theTemplateDecoratorto customize template processing.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
static ORMTemplate of(@Nonnull Connection connection, @Nonnull UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, with a custom template decorator.This method creates an ORM repository template using the provided
Connectionand applies the specified decorator to customize template processing behavior.Note: The caller is responsible for closing the connection after usage.
- Parameters:
connection- theConnectionto use for database operations; must not benull.decorator- a function that transforms theTemplateDecoratorto customize template processing.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
Returns anORMTemplatefor use with JDBC, configured with the providedStormConfig.The provided configuration is applied to the template instance, not as a process-wide default.
- Parameters:
dataSource- theDataSourceto use for database operations; must not benull.config- the Storm configuration to apply; must not benull.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
static ORMTemplate of(@Nonnull DataSource dataSource, @Nonnull StormConfig config, @Nonnull UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfigand a custom template decorator.- Parameters:
dataSource- theDataSourceto use for database operations; must not benull.config- the Storm configuration to apply; must not benull.decorator- a function that transforms theTemplateDecoratorto customize template processing.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
Returns anORMTemplatefor use with JDBC, configured with the providedStormConfig.Note: The caller is responsible for closing the connection after usage.
- Parameters:
connection- theConnectionto use for database operations; must not benull.config- the Storm configuration to apply; must not benull.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-
of
static ORMTemplate of(@Nonnull Connection connection, @Nonnull StormConfig config, @Nonnull UnaryOperator<TemplateDecorator> decorator) Returns anORMTemplatefor use with JDBC, configured with the providedStormConfigand a custom template decorator.Note: The caller is responsible for closing the connection after usage.
- Parameters:
connection- theConnectionto use for database operations; must not benull.config- the Storm configuration to apply; must not benull.decorator- a function that transforms theTemplateDecoratorto customize template processing.- Returns:
- an
ORMTemplateconfigured for use with JDBC.
-