Class Database

java.lang.Object
petclinic.util.Database

public class Database extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <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>
    find(String qlStatement, Object... qlArgs)
    Finds one or more persistent entities of a certain type in the application database.
    <E extends BaseEntity>
    E
    findById(Class<E> entityClass, int id)
    Finds an entity in the application database given its class and unique id.
    void
    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).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Database

      public Database()
  • Method Details

    • findById

      @Nullable public <E extends BaseEntity> E findById(@NonNull Class<E> entityClass, int id)
      Finds an entity in the application database given its class and unique id.
      Type Parameters:
      E - the element type
      Parameters:
      entityClass - the entity class
      id - 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 type
      qlArgs - 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, or 0 if there is no limit
      qlStatement - a JPQL "select" statement that locates entities of the same type
      qlArgs - 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

      public void save(@NonNull 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).

      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

      public void remove(@NonNull BaseEntity entity)
      Removes a given persistent entity from the application database.
      Parameters:
      entity - the entity