- All Superinterfaces:
SubqueryTemplate
- All Known Subinterfaces:
ORMTemplate
QueryTemplate relies on preview features of the Java platform:
QueryTemplaterefers to one or more preview APIs:StringTemplate.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Provides methods for constructing SQL queries and query builders using String Templates or the fluent API.
QueryTemplate is the core interface for interacting with the Storm SQL template engine. It offers two
complementary approaches to database access:
- String Templates -- Write SQL using Java String Templates with type-safe interpolation of
entity classes, records, and parameters via the
query(StringTemplate)PREVIEW method. - Query Builder -- Construct queries programmatically using the fluent
selectFrom(Class)anddeleteFrom(Class)methods, which return aQueryBuilder.
This interface also provides utility methods for accessing the SqlDialect,
creating BindVars for batch operations, and retrieving Model metadata for entity types.
Example: Query with String Template
Query query = orm.query(RAW."""
SELECT \{User.class}
FROM \{User.class}
WHERE \{User_.name} = \{"Alice"}""");
List<User> users = query.getResultList(User.class);
Example: Query with QueryBuilder
List<User> users = orm.selectFrom(User.class)
.where(User_.name, EQUALS, "Alice")
.getResultList();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionCreate a new bind variables instance that can be used to add bind variables to a batch.<T extends Data>
QueryBuilder<T, ?, ?> deleteFrom(Class<T> fromType) Creates a query builder for the specified table to delete from.st.orm.core.template.SqlDialectdialect()Get the SQL dialect for this template.Get the model for the specified recordtype.Get the model for the specified recordtype.Creates a query for the specifiedquerystring.query(StringTemplatePREVIEW template) Creates a query for the specified querytemplate.Creates a ref instance for the specified recordtypeandpk.ref(T record, ID id) Creates a ref instance for the specified recordtypeandid.default <T extends Data>
QueryBuilder<T, T, ?> selectFrom(Class<T> fromType) Creates a query builder for the specified table.default <T extends Data,R>
QueryBuilder<T, R, ?> selectFrom(Class<T> fromType, Class<R> selectType) Creates a query builder for the specified table and select type.<T extends Data,R>
QueryBuilder<T, R, ?> selectFrom(Class<T> fromType, Class<R> selectType, StringTemplatePREVIEW template) Creates a query builder for the specified table and select type using the giventemplate.Methods inherited from interface st.orm.template.SubqueryTemplate
subquery, subquery, subquery
-
Method Details
-
dialect
st.orm.core.template.SqlDialect dialect()Get the SQL dialect for this template.This method is aware of any registered
SqlInterceptorinstances and returns the SQL dialect used by the underlying SQL template. The dialect is determined based on the SQL template's configuration and the interceptors that have been applied to it.- Returns:
- the SQL dialect.
- Since:
- 1.2
-
createBindVars
BindVars createBindVars()Create a new bind variables instance that can be used to add bind variables to a batch.- Returns:
- a new bind variables instance.
- See Also:
-
ref
Creates a ref instance for the specified recordtypeandpk. This method can be used to generate ref instances for entities, projections and regular records.- Type Parameters:
T- table type.ID- primary key type.- Parameters:
type- record type.id- primary key.- Returns:
- ref instance.
- Since:
- 1.3
-
ref
Creates a ref instance for the specified recordtypeandid. This method can be used to generate ref instances for entities, projections and regular records. The object returned by this method already contains the fetched record.- Type Parameters:
T- table type.ID- primary key type.- Parameters:
id- primary key.- Returns:
- ref instance.
- Since:
- 1.3
-
model
Get the model for the specified recordtype. The model provides information about the type's database name, primary keys and columns.- Type Parameters:
T- table type.ID- primary key type.- Parameters:
type- record type.- Returns:
- the model.
-
model
Get the model for the specified recordtype. The model provides information about the type's database name, primary keys and columns.- Type Parameters:
T- table type.ID- primary key type.- Parameters:
type- record type.requirePrimaryKey- whether to require a primary key.- Returns:
- the model.
- Since:
- 1.3
-
selectFrom
Creates a query builder for the specified table.- Type Parameters:
T- the table type to select from.- Parameters:
fromType- the table to select from.- Returns:
- the query builder.
-
selectFrom
default <T extends Data,R> QueryBuilder<T,R, selectFrom?> (@Nonnull Class<T> fromType, @Nonnull Class<R> selectType) Creates a query builder for the specified table and select type.- Type Parameters:
T- the table type to select from.R- the result type.- Parameters:
fromType- the table to select from.selectType- the result type of the query.- Returns:
- the query builder.
-
selectFrom
<T extends Data,R> QueryBuilder<T,R, selectFrom?> (@Nonnull Class<T> fromType, @Nonnull Class<R> selectType, @Nonnull StringTemplatePREVIEW template) Creates a query builder for the specified table and select type using the giventemplate.- Type Parameters:
T- the table type to select from.R- the result type.- Parameters:
fromType- the table to select from.selectType- the result type of the query.template- the select clause template.- Returns:
- the query builder.
-
deleteFrom
Creates a query builder for the specified table to delete from.- Type Parameters:
T- the table type to delete from.- Parameters:
fromType- the table to delete from.- Returns:
- the query builder.
-
query
Creates a query for the specifiedquerystring.- Parameters:
query- the query.- Returns:
- the query.
-
query
Creates a query for the specified querytemplate.- Parameters:
template- the query template.- Returns:
- the query.
-
QueryTemplatewhen preview features are enabled.