1

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.

ApollonDigital
  • 913
  • 3
  • 15
  • 24

1 Answers1

1

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.

Gamlor
  • 12,978
  • 7
  • 43
  • 70
  • Thanks for the response! I guess I have a situation here ... My project is full of enums and it's very difficult to change that, or handle things without enums. I should find some hack here. Can I use somehow translators and save enums as Strings maybe? – ApollonDigital Dec 24 '12 at 11:35
  • After investing enough time on tutorials & searches about db4o I have a feeling that there are many bugs and bad designs. Im not sure even for how active project is. Sometimes I think I should start looking for something better but I don't know if there are any good alternatives. Sql solutions are low level and very strictly defined and oodbs aren't so popular and well developed ... Databases are such a mess :p – ApollonDigital Dec 24 '12 at 11:46
  • Did you end up finding a solution with db4o? Because I'm having the same problem. – rob Jun 03 '13 at 13:18