Class TransactionSession

java.lang.Object
com.codename1.backend.orm.TransactionSession
All Implemented Interfaces:
Session

public final class TransactionSession extends Object implements Session

The Session the build injects: the managed session of whatever transaction the calling thread is in.

One object is injected into a singleton and used by every request, so it cannot BE a session -- a session is a persistence context, one per unit of work, and not thread-safe. Each call is forwarded to the session of the calling thread's @Transactional method instead, which is opened on the transaction's connection when first used, flushed before the transaction commits and closed when it ends. That is the same contract as a Spring-managed EntityManager.

Outside a transaction there is no unit of work to belong to, and every call refuses with a message saying so. The transaction's boundaries belong to the annotation, so beginning, committing, rolling back and closing through this object are refused too.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Starts a transaction for this session.
    void
    Detaches every entity and discards all unflushed changes.
    void
    Rolls back any active transaction, detaches entities, and releases session resources.
    void
    Flushes pending changes and commits the active transaction.
    boolean
    contains(Object entity)
    Tests whether this session manages an entity that is not scheduled for removal.
    long
    count(Object entity, String field)
    Counts stored related rows without loading the relationship's entities.
    createQuery(String statement)
    Creates an untyped JPQL query, including bulk update or delete statements.
    <T> JpqlQuery<T>
    createQuery(String statement, Class<T> resultType)
    Creates a typed query using the supported JPQL subset.
    void
    Creates missing tables, indexes, and constraints for registered mappings.
    void
    detach(Object entity)
    Stops tracking an instance, discarding its unflushed changes.
    <T> T
    find(Class<T> type, Object id)
    Finds an entity, reusing its managed instance when present.
    <T> T
    find(Class<T> type, Object id, LockMode mode)
    Finds an entity and optionally locks its database row.
    void
    Writes pending inserts, updates, relationship changes, and removals.
    <T> boolean
    increment(Class<T> type, Object id, String field, long amount)
    Atomically adds to an integral counter in SQL after flushing pending changes.
    void
    initialize(Object entity, String field)
    Loads a relationship if it is still uninitialized.
    boolean
    isLoaded(Object entity, String field)
    Checks relationship initialization without fetching its contents.
    boolean
    Reports whether a transaction failure prevents committing.
    boolean
    Reports whether this session has an active transaction.
    void
    lock(Object entity, LockMode mode)
    Applies a lock mode to a managed entity's row.
    <T> T
    merge(T entity)
    Copies state into a managed instance, cascading through MERGE associations.
    <T> void
    persist(T entity)
    Makes a new entity managed and schedules its insertion at flush.
    <T> Query<T>
    query(Class<T> type)
    Creates a fluent entity query.
    void
    refresh(Object entity)
    Reloads a persisted managed instance, discarding its local changes.
    void
    remove(Object entity)
    Schedules a managed entity for deletion, cascading REMOVE associations.
    void
    Rolls back the active transaction and detaches all managed entities.
    void
    Checks mapped columns, type families, nullability, and primary keys.

    Methods inherited from class Object

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

    • TransactionSession

      public TransactionSession(EntityManager entities)
  • Method Details

    • createQuery

      public <T> JpqlQuery<T> createQuery(String statement, Class<T> resultType)
      Description copied from interface: Session
      Creates a typed query using the supported JPQL subset. Parsing and mapping validation occur immediately, before SQL execution.
      Specified by:
      createQuery in interface Session
      Type Parameters:
      T - result type
      Parameters:
      statement - query using entity and Java attribute names
      resultType - expected entity or scalar type; use Object[].class for tuples
      Returns:
      a query belonging to this session
    • createQuery

      public JpqlQuery<Object> createQuery(String statement)
      Description copied from interface: Session
      Creates an untyped JPQL query, including bulk update or delete statements.
      Specified by:
      createQuery in interface Session
      Parameters:
      statement - query using entity and Java attribute names
      Returns:
      a query yielding entities, scalars, or Object[] tuples
    • beginTransaction

      public void beginTransaction()
      Description copied from interface: Session
      Starts a transaction for this session.
      Specified by:
      beginTransaction in interface Session
    • commitTransaction

      public void commitTransaction()
      Description copied from interface: Session
      Flushes pending changes and commits the active transaction. A failure leaves a still-active database transaction requiring rollback. If the database already ended the failed transaction, the session detaches its entities and becomes inactive; a new transaction can then be started.
      Specified by:
      commitTransaction in interface Session
    • rollbackTransaction

      public void rollbackTransaction()
      Description copied from interface: Session
      Rolls back the active transaction and detaches all managed entities. Pending changes are discarded; Java field values are not reverted.
      Specified by:
      rollbackTransaction in interface Session
    • isTransactionActive

      public boolean isTransactionActive()
      Description copied from interface: Session
      Reports whether this session has an active transaction.
      Specified by:
      isTransactionActive in interface Session
      Returns:
      true between a successful begin and commit or rollback
    • isRollbackOnly

      public boolean isRollbackOnly()
      Description copied from interface: Session
      Reports whether a transaction failure prevents committing.
      Specified by:
      isRollbackOnly in interface Session
      Returns:
      true when the active transaction must be rolled back
    • contains

      public boolean contains(Object entity)
      Description copied from interface: Session
      Tests whether this session manages an entity that is not scheduled for removal.
      Specified by:
      contains in interface Session
      Parameters:
      entity - instance to test; null is allowed
      Returns:
      true if the instance is currently managed and not removed
    • detach

      public void detach(Object entity)
      Description copied from interface: Session
      Stops tracking an instance, discarding its unflushed changes. Cascades DETACH only through already loaded relationships. Does nothing for an instance that is not managed by this session.
      Specified by:
      detach in interface Session
      Parameters:
      entity - instance to detach
    • clear

      public void clear()
      Description copied from interface: Session
      Detaches every entity and discards all unflushed changes. Does not end the transaction or undo SQL already executed in it.
      Specified by:
      clear in interface Session
    • close

      public void close()
      Description copied from interface: Session
      Rolls back any active transaction, detaches entities, and releases session resources. Repeated calls have no effect. The entity manager retains ownership of its database.
      Specified by:
      close in interface Session
    • find

      public <T> T find(Class<T> type, Object id)
      Description copied from interface: Session
      Finds an entity, reusing its managed instance when present. An uncached lookup flushes pending changes in an active transaction.
      Specified by:
      find in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      type - mapped entity class
      id - non-null scalar key, embedded key, or composite Identifier
      Returns:
      the managed entity, or null if no matching row exists
    • find

      public <T> T find(Class<T> type, Object id, LockMode mode)
      Description copied from interface: Session
      Finds an entity and optionally locks its database row. A pessimistic lock requires an active transaction and a supporting backend.
      Specified by:
      find in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      type - mapped entity class
      id - entity identifier
      mode - requested lock mode
      Returns:
      the managed entity, or null if no matching row exists
    • lock

      public void lock(Object entity, LockMode mode)
      Description copied from interface: Session
      Applies a lock mode to a managed entity's row.
      Specified by:
      lock in interface Session
      Parameters:
      entity - managed instance
      mode - requested lock mode; pessimistic modes require an active transaction
    • persist

      public <T> void persist(T entity)
      Description copied from interface: Session
      Makes a new entity managed and schedules its insertion at flush. Traverses associations with PERSIST cascade. A to-one reference to an unsaved entity without that cascade is rejected at flush.
      Specified by:
      persist in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      entity - new mapped instance
    • merge

      public <T> T merge(T entity)
      Description copied from interface: Session
      Copies state into a managed instance, cascading through MERGE associations. Continue working with the returned instance; the supplied detached instance does not become managed merely because it was passed to this method.
      Specified by:
      merge in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      entity - new or detached mapped instance
      Returns:
      the managed instance containing the merged state
    • remove

      public void remove(Object entity)
      Description copied from interface: Session
      Schedules a managed entity for deletion, cascading REMOVE associations.
      Specified by:
      remove in interface Session
      Parameters:
      entity - managed instance to remove
    • refresh

      public void refresh(Object entity)
      Description copied from interface: Session
      Reloads a persisted managed instance, discarding its local changes. Traverses REFRESH cascades. Rejects new, unflushed instances before changing session state. A failure after reload begins clears the context and marks an active transaction rollback-only.
      Specified by:
      refresh in interface Session
      Parameters:
      entity - persisted instance managed by this session
    • flush

      public void flush()
      Description copied from interface: Session
      Writes pending inserts, updates, relationship changes, and removals. Does not commit. Failure marks the active transaction rollback-only.
      Specified by:
      flush in interface Session
    • increment

      public <T> boolean increment(Class<T> type, Object id, String field, long amount)
      Description copied from interface: Session
      Atomically adds to an integral counter in SQL after flushing pending changes. Also increments an optimistic version when present and refreshes a managed instance of the affected row. Guards against counter and version overflow.
      Specified by:
      increment in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      type - mapped entity class
      id - entity identifier
      field - Java name of an int or long field that is neither key nor version
      amount - signed amount to add
      Returns:
      true if a row changed; false for a missing row, null counter, or overflow
    • query

      public <T> Query<T> query(Class<T> type)
      Description copied from interface: Session
      Creates a fluent entity query.
      Specified by:
      query in interface Session
      Type Parameters:
      T - entity type
      Parameters:
      type - mapped entity class
      Returns:
      an initially unrestricted query
    • createTables

      public void createTables()
      Description copied from interface: Session
      Creates missing tables, indexes, and constraints for registered mappings. Run outside application transactions. Does not migrate existing tables.
      Specified by:
      createTables in interface Session
    • validateSchema

      public void validateSchema()
      Description copied from interface: Session
      Checks mapped columns, type families, nullability, and primary keys. Does not migrate the schema or exhaustively validate indexes and foreign keys.
      Specified by:
      validateSchema in interface Session
    • count

      public long count(Object entity, String field)
      Description copied from interface: Session
      Counts stored related rows without loading the relationship's entities. Flushes pending changes when a transaction is active. Detached owners are identified by their persisted key; local detached relationship edits are ignored.
      Specified by:
      count in interface Session
      Parameters:
      entity - owner with a persisted identifier
      field - Java name of the relationship or element collection
      Returns:
      relationship size; zero or one for a to-one association
    • isLoaded

      public boolean isLoaded(Object entity, String field)
      Description copied from interface: Session
      Checks relationship initialization without fetching its contents.
      Specified by:
      isLoaded in interface Session
      Parameters:
      entity - mapped instance
      field - Java relationship name
      Returns:
      true if loaded or if the instance has no managed lazy state
    • initialize

      public void initialize(Object entity, String field)
      Description copied from interface: Session
      Loads a relationship if it is still uninitialized.
      Specified by:
      initialize in interface Session
      Parameters:
      entity - relationship owner
      field - Java relationship name