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 & Spring MVC

Last post 02-08-2008, 10:25 PM by gishac. 2 replies.
Sort Posts: Previous Next
  •  02-08-2008, 08:46 PM 46659

    DB4O & Spring MVC

    ciao a tutti!

    sto sviluppando un'applicazione web basata su Spring MVC e utilizzando DB4O come database. Il problema è che a volte (ed in maniera apparentemente casuale) quando il framework esegue l'istruzione

                    Studente studente = (Studente) database.getContainer().ext().getByID(studenteId);

    viene fuori un oggetto che ha tutti i campi a null, come testimoniato dalla stampa successiva

                    System.out.println("**************** " + database.getContainer().ext().getID(studente) + " " + studente.getNome() + " " + studente.getCognome() + " " + studente.getAmico());

    Invece, eseguendo questo tipo di query

            Studente studente_prototipo = new Studente();
            ObjectSet<Studente> studenti = database.getContainer().get( studente_prototipo );

    questo problema non si verifica mai.

     
    Ho fatto prove anche con altri tipi di campi (int, double, ecc.) ma sembra che quando l'errore di caricamento si verifica, anche questi ultimi vengano riempiti con il loro valore di default, che è 0.

    quale potrebbe essere la causa? forse sto sbagliando modo di fare la query? il problema è stato affrontato e risolto da qualcuno?
     

    public class Studente {
            private String nome;
            private String cognome;

            private Studente amico;

            public Studente()
            {
            }

            public Studente(String nome, String cognome)
            {
                    this.nome = nome;
                    this.cognome = cognome;
            }

            public String getNome() {
                    return nome;
            }

            public void setNome(String nome) {
                    this.nome = nome;
            }

            public String getCognome() {
                    return cognome;
            }

            public void setCognome(String cognome) {
                    this.cognome = cognome;
            }

            public Studente getAmico()
            {
                    return amico;
            }

            public void setAmico( Studente amico )
            {
                    this.amico = amico;
            }


    }

     

    public class StudenteController_Dettagli implements Controller
    {

            private Database database;

            public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception
            {
                    int studenteId = Integer.parseInt(arg0.getParameter("id"));
                    System.out.println(studenteId);
                    Studente studente = (Studente) database.getContainer().ext().getByID(studenteId);
                    System.out.println("**************** " + database.getContainer().ext().getID(studente) + " " + studente.getNome() + " " + studente.getCognome());
                    ModelAndView mav = new ModelAndView("Studente_Dettagli""studente", studente);
                    return mav;
            }

            public Database getDatabase()
            {
                    return database;
            }

            public void setDatabase( Database database )
            {
                    this.database = database;
            }

    }

  •  02-08-2008, 09:48 PM 46662 in reply to 46659

    Re: DB4O & Spring MVC

    ciao a tutti!

    sembrerebbe che il metodo getByID() non faccia esattamente il suo dovere in quel contesto, nel senso che ritorna un oggetto istanziato ma non istanzia i campi interni, ma in maniera apparentemente casuale. mentre se viene richiamato da altri controller sembra funzionare a dovere.

    sostituendo quel tipo di query con una query by example del tipo

    Studente studente_prototipo = new Studente();
    studente_prototipo.setNome("Pinco");
    ObjectSet<Studente> listaStudenti = database.getContainer().get(studente_prototipo);
    Studente studente = null;
    if (listaStudenti.hasNext())
    {
        studente = listaStudenti.next();
    }

    funziona sempre.
     

  •  02-08-2008, 10:25 PM 46667 in reply to 46662

    Re: DB4O & Spring MVC

    After you get the object with the GetByID method you have to activate it in order to load all the information about the object, if you don't activate it you only will have the reference to the object with all the fields null,

    try this:

    Studente studente = (Studente) database.getContainer().ext().getByID(studenteId);

    database.getContainer().Activate( studente, Activationdepth);


    salu2 desde Ecuador
    Gisbert Avellan
View as RSS news feed in XML