Transient Fields In Java

This topic applies to Java version only

You can use the transient keyword to indicate that a field is not part of the persistent state of an object:

Test.java
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.selpersist; 04 05 06public class Test { 07 transient String transientField; 08 String persistentField; 09 10 public Test(String transientField, String persistentField) 11 { 12 this.transientField = transientField; 13 this.persistentField = persistentField; 14 } 15 16 public String toString() 17 { 18 return "Test: persistent: " + persistentField + ", transient: " + transientField ; 19 } 20 21}

The following example demonstrates the effect of transient keyword on db4o:

MarkTransientExample.java: saveObjects
01public static void saveObjects(){ 02 new File(YAPFILENAME).delete(); 03 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 04 try 05 { 06 Test test = new Test("Transient string","Persistent string"); 07 oc.set(test); 08 } 09 finally 10 { 11 oc.close(); 12 } 13 }
MarkTransientExample.java: retrieveObjects
01public static void retrieveObjects() 02 { 03 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 04 try 05 { 06 ObjectSet result = oc.query(Test.class); 07 listResult(result); 08 } 09 finally 10 { 11 oc.close(); 12 } 13 }