TTransient Translator

TTransient translator should be used with the objects that must not be stored to db4o. These can be classes holding internal references to the runtime or system.

A simple Java example can look as follows.

BuiltInTranslatorsExample.java: saveTransient
01public static void saveTransient() 02 { 03 new File(DB4O_FILE_NAME).delete(); 04 Configuration configuration = Db4o.newConfiguration(); 05 // Configure NotStorable class with TTransient translator to prevent its storage 06 configuration.objectClass(NotStorable.class).translate(new TTransient()); 07 ObjectContainer container = database(configuration); 08 if (container != null) 09 { 10 try 11 { 12 Item item = new Item("Test1", new NotStorable("test1")); 13 container.set(item); 14 item = new Item("Test2", new NotStorable("test2")); 15 container.set(item); 16 } 17 catch (Db4oException ex) 18 { 19 ex.printStackTrace(); 20 } 21 catch (Exception ex) 22 { 23 ex.printStackTrace(); 24 } 25 finally 26 { 27 closeDatabase(); 28 } 29 } 30 }

BuiltInTranslatorsExample.java: testTTransient
01public static void testTTransient() 02 { 03 saveTransient(); 04 ObjectContainer container = database(); 05 if (container != null) 06 { 07 try 08 { 09 // Trying to retrieve 10 ObjectSet result = container.query(Item.class); 11 // NotStorable class is configured transient, so we will 12 // see null values for _ns field. 13 listResult(result); 14 } 15 catch (Db4oException ex) 16 { 17 ex.printStackTrace(); 18 } 19 catch (Exception ex) 20 { 21 ex.printStackTrace(); 22 } 23 finally 24 { 25 closeDatabase(); 26 } 27 } 28 }
 

Item.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.builtintranslators; 04 05 06public class Item { 07 String _name; 08 NotStorable _ns; 09 10 public Item(String name, NotStorable ns) { 11 _name = name; 12 _ns = ns; 13 } 14 15 public String toString() { 16 return _name + "/" + (_ns == null ? "null" : _ns); 17 } 18}
 

NotStorable.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.builtintranslators; 04 05public class NotStorable { 06 String _name; 07 08 public NotStorable(String name) { 09 _name = name; 10 } 11 12 public String toString() { 13 return _name == null ? "null" : _name; 14 } 15}

Item class contains NotStorable field. NotStorable object is translated with TTransient translator, which prevents its instances from being stored.