By default db4o does not persist static fields. This is not necessary because static values are set for a class, not for an object. However you can set up db4o to store static fields if you want to implement constants or enumeration: See "Persist Static Fields"
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().objectClass(Person.class).persistStaticFieldValues();
When this setting is enabled, all non-primitive-typed static fields are stored the first time an instance of the class is stored. The values are restored every time a database file is opened afterwards, after the class meta information is loaded for this class (when the class objects are retrieved with a query, for example).
Use this option with caution. This option means that static fields are stored in the database. When you change the value of this field, you need to store it explicitly again. Furthermore, db4o will replace the static value at runtime, which can lead to very subtle bugs in your application.
This option does not have any effect on primitive types like ints, longs, floats etc.
One of the use-cases is when you have an enumeration class which you want to store. In fact, Java enums implement this enumeration class idiom and db4o persist the static fields or all enums. For example we have a color class, which also has some static colors.
public final class Color { public final static Color BLACK = new Color(0,0,0); public final static Color WHITE = new Color(255,255,255); public final static Color RED = new Color(255,0,0); public final static Color GREEN = new Color(0,255,0); public final static Color BLUE = new Color(0,0,255); private final int red; private final int green; private final int blue; private Color(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; } public int getRed() { return red; } public int getGreen() { return green; } public int getBlue() { return blue; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Color color = (Color) o; if (blue != color.blue) return false; if (green != color.green) return false; if (red != color.red) return false; return true; } @Override public int hashCode() { int result = red; result = 31 * result + green; result = 31 * result + blue; return result; } @Override public String toString() { return "Color{" + "red=" + red + ", green=" + green + ", blue=" + blue + '}'; } }
We want to ensure reference equality on colors so that you easily can check for a certain color. But when we load the colors from the database you get new instances and not the same instance as in the static field. This means that comparing the references will fail.
// When you enable persist static field values, you can compare by reference // because db4o stores the static field if(car.getColor()== Color.BLACK){ System.out.println("Black cars are boring"); } else if(car.getColor()== Color.RED){ System.out.println("Fire engine?"); }
When you enable the persist static fields option, the static fields are stored. This means that the object referenced in the static fields are loaded from the database and therefore the same instance. And the comparing the references works again.
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().objectClass(Color.class).persistStaticFieldValues();