Class DBRecord

All Implemented Interfaces:
Serializable, Cloneable, Record, RecordData, DBContextAware
Direct Known Subclasses:
TRecord

public class DBRecord extends DBRecordBase
This class represents a record from a database table, view or query The class provides methods to create, read, update and delete records If an Idendity-column (AUTOINC) is defined, the value will be set upon creation by the dbms to the next value If a Timestamp-column is defined the value will be automatically set and concurrent changes of the record will be detected If changes to the record are made, but a rollback on the connection is performed, the changes will be reverted (Rollback-Handling) The record is Serializable either if the provided DBContext is serializable, or if the Context is provided on deserialization in a derived class.
See Also:
  • Field Details

    • context

      protected final transient DBContext context
    • rowset

      protected final transient DBRowSet rowset
  • Constructor Details

    • DBRecord

      protected DBRecord(DBContext context, DBRowSet rowset, boolean enableRollbackHandling)
      Internal constructor for DBRecord May be used by derived classes to provide special behaviour
      Parameters:
      context - the database context
      rowset - the rowset to which this record belongs
      enableRollbackHandling - flag whether to enable rollback handling
    • DBRecord

      public DBRecord(DBContext context, DBRowSet rowset)
      Constructs a new DBRecord.
      Parameters:
      context - the DBContext for this record
      rowset - the corresponding RowSet(Table, View, Query, etc.)
  • Method Details

    • key

      public static Object[] key(Object... values)
      varArgs to Array
      Parameters:
      values - the key values
      Returns:
      the key
    • writeContext

      protected void writeContext(ObjectOutputStream strm) throws IOException
      Throws:
      IOException
    • writeRowSet

      protected void writeRowSet(ObjectOutputStream strm) throws IOException
      Throws:
      IOException
    • readContext

      protected DBContext readContext(ObjectInputStream strm) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException
    • readRowSet

      protected DBRowSet readRowSet(ObjectInputStream strm) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException
    • getContext

      public DBContext getContext()
      Returns the current Context
      Returns:
      the database context
    • getRowSet

      public DBRowSet getRowSet()
      Returns the DBRowSet object.
      Specified by:
      getRowSet in class DBRecordBase
      Returns:
      the DBRowSet object
    • isRollbackHandlingEnabled

      public boolean isRollbackHandlingEnabled()
      Returns whether or not RollbackHandling is enabled for this record
      Specified by:
      isRollbackHandlingEnabled in class DBRecordBase
      Returns:
      true if rollback handling is enabled or false otherwise
    • setRollbackHandlingEnabled

      public void setRollbackHandlingEnabled(boolean enabled)
      Set whether or not RollbackHandling will be performed for this record Since Rollback handling requires additional resources it should only be used if necessary Especially for bulk operations it should be disabled
      Parameters:
      enabled - flag whether to enable or disable RollbackHandling
    • getIdentity

      public long getIdentity()
      Returns the record identity for tables which have a single numeric primary key like AUTOINC This method is provided for convenience in addition to the the getKey() method
      Returns:
      the record id or 0 if the key is null
      Throws:
      NoPrimaryKeyException - if the table has no primary key
      NotSupportedException - if the primary key is not a single column of if the column is not numeric
    • create

      public DBRecord create(Object[] initalKey)
      Creates a new record
      Parameters:
      initalKey - the record key
      Returns:
      returns self (this)
    • create

      public DBRecord create()
      Creates a new record
      Returns:
      returns self (this)
    • read

      public DBRecord read(Object[] key)
      Reads a record from the database
      Parameters:
      key - an array of the primary key values
      Returns:
      returns self (this)
      Throws:
      NoPrimaryKeyException - if the associated RowSet has no primary key
      InvalidKeyException - if the key does not match the key columns of the associated RowSet
    • read

      public DBRecord read(Object id)
      Reads a record from the database This method can only be used for tables with a single primary key
      Parameters:
      id - the primary key of the record
      Returns:
      returns self (this)
      Throws:
      NoPrimaryKeyException - if the associated RowSet has no primary key
      InvalidKeyException - if the associated RowSet does not have a single column primary key
    • read

      public DBRecord read(DBCompareExpr whereConstraints)
      Reads a record from the database
      Parameters:
      whereConstraints - the compare expression for querying this record
      Returns:
      returns self (this)
    • read

      public DBRecord read(Object[] key, DBRowSet.PartialMode mode, DBColumn... columns)
      Reads a record partially i.e. not with all but just some selected fields There are two modes: 1. PartialMode.INCLUDE reads only the fields provided with the column list 2. PartialMode.EXCLUDE reads all but the fields provided with the column list The primary key is always fetched implicitly
      Parameters:
      key - the primary key values
      mode - flag whether to include only the given columns or whether to add all but the given columns
      columns - the columns to include or exclude (depending on mode)
      Returns:
      returns self (this)
    • set

      public DBRecord set(Column column, Object value)
      Overridden to change return type from DBCommandExpr to DBCommand
      Specified by:
      set in interface Record
      Overrides:
      set in class DBRecordBase
      Parameters:
      column - a DBColumn object
      value - the value
      Returns:
      returns self (this)
    • update

      public void update()
      Updates the record and saves all changes in the database.
    • delete

      public void delete()
      This helper function calls the DBRowset.deleteRecord method to delete the record. WARING: There is no guarantee that it ist called Implement delete logic in the table's deleteRecord method if possible
      See Also: