Creating and Modifying Database Objects in Java

Java applications use the new operator to instantiate an object and the assignment operator to modify the objects values. To create the database store of the object the Connection method insert() is called within a transaction. For example:

 
    @Persistent // Class Record will be stored in eXtremeDB database
    class Record
    {
        @Indexable(unique=true) // Create unique (tree) eXtremeDB index by "id" field
        int id;
 
        public String str;
    }
    ...
    Connection con = new Connection(db);
    con.startTransaction(Database.TransactionType.ReadWrite); // Start RW transaction
    Record rec = new Record();
    rec.id = 3;
    rec.str = "Record 3";
    con.insert(rec);
    con.commitTransaction();
     

To update an object, it is first found with a Cursor navigation, then its fields are modified and the Cursor method update() is called with the object handle. For example:

 
    cursor = new Cursor<Record>(con, "id");
    // find record with id == 3
    rec = cursor.find(3);
    rec.str = "Updated string"; 
    cursor.update();
    cursor.close(); //release cursor
    con.CommitTransaction();
     

To delete an object from the database, the object is first found with a Cursor navigation, then the Cursor method remove() is called with the object handle. For example:

 
    cursor = new Cursor<Record>(con, "id");
    // find record with id == 3
    rec = cursor.Find(3);
    cursor.remove(); // remove current object (pointed to by the cursor)
    cursor.close(); //release cursor
    con.CommitTransaction();
     

Strings and Arrays

The Length property of String fields can be used to determine their actual size in bytes. Also, since vectors (aka dynamic arrays) in eXtremeDB are simply declared with the native language array syntax, the fields Length property gives the number of vector elements.

In Java applications, the elements of vector (array) fields are accessed using the normal array element operator [] and specifying the index of the desired element.

Nullable Fields

A Java class field can be explicitly declared nullable with the @nullable annotation. For example:

 
    class Record
    {
        @Indexable(Unique=true) // Create unique (tree) eXtremeDB index by "id" field
        public int id;
     
        @Nullable
        public int weight;
    }
 

Here field weight may have null values.

Please see the Nullable Fields and Indexes page for an explanation of the behavior of nullable fields included in indexes.

Blob Support

Java applications use the @Blob field annotation to define a BLOB field.

Date, Time and Datetime Fields

Please refer to the Datetime FIelds page for a detailed description of the eXtremeDB date, time and datetime database field types. The Java APIs for determining precision and accessing date, time and datetime fields are described in the Managing Datetime Fields in Java page.