db4o Developer Community

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

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

  •  09-04-2008, 08:03 PM

    • douglascrp is not online. Last active: 24/11/2008, 02:32 AM douglascrp
    • Not Ranked
    • Joined on 07-10-2008
    • Sã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 Complete Thread