db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

[Db4o 5.5 Java] Enum "name" is null.

Last post 09-04-2008, 08:03 PM by douglascrp. 2 replies.
Sort Posts: Previous Next
  •  02-17-2007, 10:45 AM 34059

    [Db4o 5.5 Java] Enum "name" is null.

    Hello,

    I'm trying to retrieve a previously stored list of Java enums from my database, but I find the name to be null.
    The enums are stored in a list which is a fields of another object - the entire structure looks roughly like:

    public class container{
        private final InnerContainer inner;
    }

    public class InnerContainer{
       private final List<EnumClass> list;
    }

    "cascadeOnUpdate" is set to be true for both class and InnerContainer.class, in addition, I have enabled "persistStaticValues" for the EnumClass.
    When examining the the stored instances of the enum in the db4o manager, their name and ordinal are both "null" (mind that ordinal is a primitive int, shouldn't be possible to be null.) , but when I look at them in a debugger (eclipse's standard debugger, in case it matters), the ordinal is set correctly.
    Moreover, having a database with those wrong values stored breaks this enum entirely, since all references to those values that were previously stored return "null" as their name.
    My question is twofold: For one, I'd like to know whether there is a way to retrieve and re-validate the existing database, or whether stored data is lost. In addition, it would be great if you could tell me how to avoid this problem in the future.

    Thanks a lot
    -Urs

     

    Filed under: ,
  •  09-04-2008, 01:05 PM 50898 in reply to 34059

    • douglascrp is not online. Last active: 24/11/2008, 02:32 AM douglascrp
    • Not Ranked
    • Joined on 07-10-2008
    • S&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;#227;o Paulo - Brasil
    • Posts 5

    Re: [Db4o 5.5 Java] Enum "name" is null.

    Goog morning.

    This problem is happening with me too, but I'm using the 7.2 version of the db4o.

    Ursulus, this problem was solved? If yes, can you say me how?

    Thank you in advance and sorry for my bad English...

    --

    Douglas C. R. Paes

    http://douglascrp.blogspot.com


    Douglas C. R. Paes
    blog: http://douglascrp.blogspot.com
    msn: douglascrp@gmail.com
    skype: douglascrp

    The two basic principles of Windows system administration:

    * For minor problems, reboot
    * For major problems, reinstall
    Filed under:
  •  09-04-2008, 08:03 PM 50907 in reply to 50898

    • douglascrp is not online. Last active: 24/11/2008, 02:32 AM douglascrp
    • Not Ranked
    • Joined on 07-10-2008
    • S&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;#227;o Paulo - Brasil
    • Posts 5

    [Solved] Re: [Db4o 5.5 Java] Enum "name" is null.

    this is my solution...

     

    import java.lang.reflect.Field;
    import java.util.List;

    import br.com.entrepostosilk.Empresa;
    import br.com.entrepostosilk.db4objects.Db4oUtil;

    import com.db4o.query.Predicate;

    public class CorretorBanco {

        public static void main(String[] args) {
            CorretorBanco corretor = new CorretorBanco();
            
            corretor.corrigirBanco();
        }
        
        private void corrigirBanco() {
            try {
                // field declared in the Empresa class
                Field fieldNome = Empresa.class.getDeclaredField("nome");
                fieldNome.setAccessible(true);
                
                // inherited from java.lang.Enum
                // note the getSuperclass().getDeclaredField("name") and getSuperclass().getDeclaredField("ordinal")
                Field fieldName = Empresa.class.getSuperclass().getDeclaredField("name");
                fieldName.setAccessible(true);
                Field fieldOrdinal = Empresa.class.getSuperclass().getDeclaredField("ordinal");
                fieldOrdinal.setAccessible(true);
                
                // there are only two values in Empresa enum, ENTREPOSTO and TALYS
                for (Empresa empresa : getAll()) {
                    if (empresa.getNome().equals("Entreposto")) {
                        
                        fieldNome.set(empresa, "ENTREPOSTO");
                        fieldName.set(empresa, "ENTREPOSTO");
                        fieldOrdinal.setInt(empresa, 0);
                        
                    } else {
                        if (empresa.getNome().equals("Talys")) {
                        
                            fieldNome.set(empresa, "TALYS");
                            fieldName.set(empresa, "TALYS");
                            fieldOrdinal.setInt(empresa, 1);
                            
                        }    
                    }
                    
                    Db4oUtil.getObjectContainer().store(empresa);
                    Db4oUtil.getObjectContainer().commit();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
        
        @SuppressWarnings("serial")
        public List<Empresa> getAll() {

            List<Empresa> list = Db4oUtil.getObjectContainer().query(new Predicate<Empresa>() {
                public boolean match(Empresa empresa) {
                    return true;
                }
            });
            
            return list;
        }
        
    }


    Douglas C. R. Paes
    blog: http://douglascrp.blogspot.com
    msn: douglascrp@gmail.com
    skype: douglascrp

    The two basic principles of Windows system administration:

    * For minor problems, reboot
    * For major problems, reinstall
    Filed under: ,
View as RSS news feed in XML