db4o enumset exception

310 Views Asked by At

I'm using db4o 8.0 with transparent activation/persistence ... I have a class which contains an EnumSet (and other things). I instantiate, add an enum value into set and store. When I search in DB, get it and trying to activate the object I get this exception:

Exception in thread "main" java.lang.ClassCastException: class Resources.Enums$fooEnum_t != null
    at java.util.EnumSet.typeCheck(EnumSet.java:380)
    at java.util.RegularEnumSet.add(RegularEnumSet.java:160)
    at java.util.RegularEnumSet.add(RegularEnumSet.java:36)
    at com.db4o.typehandlers.CollectionTypeHandler.addToCollection(CollectionTypeHandler.java:120)
    at com.db4o.typehandlers.CollectionTypeHandler.activate(CollectionTypeHandler.java:45)
    at com.db4o.internal.Handlers4.activate(Handlers4.java:300)
...
...

To store enumSet into db4o I use:

config.common().objectClass(EnumSet.class).translate(new com.db4o.config.TSerializable());

I can't figure out what the problem is. Any ideas?

EDIT:
I don't know where to ask questions about db4o. SO community doesn't seem very active about db4o. Is this because db4o isn't very popular or there is another place for "support"?

EDIT 2:
I've found that post on versant's forum which may be relevant but doesn't help: http://community.versant.com/Forums/tabid/98/aft/1046/Default.aspx#3370

I recognized one more thing. There is no built in support for java.util.EnumMap and java.util.EnumSet, db4o will throw an exception because there is no default constructor and all the constructors with null args will fail for these classes. Db4o.configure().objectClass("java.util.EnumMap").translate(new com.db4o.config.TSerializable()) will solve the problem of course. I think the standard collection framework should be supported.

1

There are 1 best solutions below

3
On

I even would be careful with using enums in db4o. The way db4o stores enum is extremely dangerous. In case you refactor, change the enum, it might lead to super strange behavior, bugs when you stored that enum in db4o. It goes so far that switch statements on enums might jump to the wrong place.

Short reason: db4o stores enums like a object instance, and sets the enum values per reflection when loaded, this can lead to extremely weird bugs, since a enum is not supposed to change.

I would avoid storing (Java)-enums, and therefore also the enum-set.