Package petclinic.util
Class Database
java.lang.Object
petclinic.util.Database
Provides access to the application database, allowing transient instances of entity classes to be persisted, and
persistent instances to be recovered or removed from the database.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<E extends BaseEntity>
List<E> find
(@org.checkerframework.checker.index.qual.NonNegative int maxResults, String qlStatement, Object... qlArgs) Finds one or more persistent entities of a certain type in the application database, up to a given maximum number of entities.<E extends BaseEntity>
List<E> Finds one or more persistent entities of a certain type in the application database.<E extends BaseEntity>
EFinds an entity in the application database given its class and unique id.void
remove
(BaseEntity entity) Removes a given persistent entity from the application database.void
save
(BaseEntity entity) Saves the state of a given entity to the application database, whether it is new (still transient, with no id) or already persisted (with an id).
-
Constructor Details
-
Database
public Database()
-
-
Method Details
-
findById
Finds an entity in the application database given its class and unique id.- Type Parameters:
E
- the element type- Parameters:
entityClass
- the entity classid
- the id- Returns:
- the persistent entity if found, or
null
if not found
-
find
@NonNull public <E extends BaseEntity> List<E> find(@NonNull String qlStatement, @NonNull Object... qlArgs) Finds one or more persistent entities of a certain type in the application database.- Type Parameters:
E
- the element type- Parameters:
qlStatement
- a JPQL "select" statement that locates entities of the same typeqlArgs
- zero or more argument values for the positional query parameters specified in the JPQL statement, in the same order as the parameter positions- Returns:
- the list of zero or more entities found, in an arbitrary order or in the order specified by an "order by" clause (if any)
- See Also:
-
find
@NonNull public <E extends BaseEntity> List<E> find(@org.checkerframework.checker.index.qual.NonNegative int maxResults, @NonNull String qlStatement, @NonNull Object... qlArgs) Finds one or more persistent entities of a certain type in the application database, up to a given maximum number of entities.- Type Parameters:
E
- the element type- Parameters:
maxResults
- the maximum number of resulting entities to be returned, or0
if there is no limitqlStatement
- a JPQL "select" statement that locates entities of the same typeqlArgs
- zero or more argument values for the positional query parameters specified in the JPQL statement, in the same order as the parameter positions- Returns:
- the list of zero or more entities found, in an arbitrary order or in the order specified by an "order by" clause (if any)
-
save
Saves the state of a given entity to the application database, whether it is new (still transient, with no id) or already persisted (with an id).In either case, the persistence context is synchronized to the application database, so that any pending "inserts", "updates" or "deletes" get executed at this time.
- Parameters:
entity
- the entity
-
remove
Removes a given persistent entity from the application database.- Parameters:
entity
- the entity
-