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

Problem with duplicating objects

Last post 09-23-2006, 12:02 AM by Tuna Toksoz. 9 replies.
Sort Posts: Previous Next
  •  09-21-2006, 01:00 PM 29406

    Problem with duplicating objects

    OK, here is another question from beginner, gurus should solve this easily. :)

     
    I have two classes: Article and User. The Article class has one User object attribute. So it's like RDBMS's one-to-many relationship. One user can be found in many articles.

    When the user tries to log on into website (jsp), there is method doesUserExist that returns object of class User if it does exist, and null if it doesn't.

    I store the object that is returned in http session variable "userloggedin". Now, when I make new instance of Article class, db4o also makes a new instance of User too and permanently stores it, with new unique ID, which I don't want to happen. So, somehow, I just need to store reference to object (maybe unique object ID?) of User in Article object. How to do this, I think I found on this forum that if I play with unique ID's like references, I actually don't use the power od db4o. Also, I read that this can happen if ObjectContainer is closed, which is not the case. Btw, I'm using Db4oUtil.java as a helper for using db4o with jsp pages.

    Please help,  

    Marko 

  •  09-21-2006, 06:00 PM 29419 in reply to 29406

    Re: Problem with duplicating objects

    When your Articule object constructs the user reference, it has to go get it to the DB to get it ( not construct the exact atribute-based object then the one who resides in db4o ), on the other hand, sometime, you have to handle the identity of the objects a little more than just having and ID.

    I hope it helps.
    Alan.


    Alan Lavintman
  •  09-22-2006, 09:08 AM 29440 in reply to 29419

    Re: Problem with duplicating objects

    But I'm not constructing the same object. I get it from database, store in session variable, and then put it in new Article object, as needed. Here's important code:

    check if User object is stored in database, and if User is returned, stores it in session variable.

    korbean.kor = korbean.postojiLiKorisnikLogin(email,password);
    if (korbean.kor != null) {
    session.setAttribute("userloggedin", korbean.kor);
    session.setAttribute("username", request.getParameter("email"));
    }

     

    method that returns object Korisnik (actually User), if one exists

    public Korisnik postojiLiKorisnikLogin(String email, String password) {
    Korisnik prototip = new Korisnik(email,password,null,null);
    ObjectContainer oc = Db4oUtil.getObjectContainer();
    ObjectSet rezultat = oc.get(prototip);
    if (!rezultat.isEmpty()) { return (Korisnik)rezultat.get(0); }
    else return null;
    }

     

    method that adds new Article to database, you see the Korisnik (User) object at the end...

    public void dodajArtikal(String proizvodjac, String model, double cena, char transakcija, char stanje, Korisnik korisnik) {
    Artikal a = new Artikal (proizvodjac, model, cena, transakcija, stanje, korisnik);
    ObjectContainer oc = Db4oUtil.getObjectContainer();
    oc.set(a);
    Db4oUtil.closeObjectContainer();
    }

     

    I'm stuck, everything is logical to me, what to say else .. :) And I'm nervous, I was thinking this whole thing with db4o would be easier to me.. :(

  •  09-22-2006, 09:23 AM 29442 in reply to 29406

    Re: Problem with duplicating objects

    are you closing the db betwen operations?
    Eric Falsken » db4objects
  •  09-22-2006, 10:10 AM 29445 in reply to 29442

    Re: Problem with duplicating objects

    Eric Falsken:
    are you closing the db betwen operations?

    I don't think it matters. If an object is stored in session variable it means that the object is serialized and then deserialized. This means you get a new object that has the same values as the original object but is not the same object. The only way around this is to:

    a) Bind the object to his db id.

    b)  Retrieve the original object from the db.

    My preference is to use a readonly id field that is set to db id on activation and bind the object to that id whenever i need a persistence operation.

    Goran
     

  •  09-22-2006, 10:45 AM 29448 in reply to 29445

    Re: Problem with duplicating objects

    So I see it is possible that I must use db id's, when it comes to storing related objects in session variables? This actually comes really close to RDBMS way of thinking which is not something I want to look back to. I don't want to bother with any id's anymore... Is it possible that whole HTTP session thing ruins the ODBMS idea?

    btw, yes... the connection to database is closed automatically by servlet filter, after each request.

    I think the solution a) is better than b), because it's more focused on the job (does not store whole object, and there's no overhead of finding the original object). Again, it's half-RDBMS, definitely, which is bad.

     
    Will try to use ID's and I'll post my findings here...  Thank you all.  
     

  •  09-22-2006, 05:08 PM 29473 in reply to 29448

    Re: Problem with duplicating objects

    mmhh i think that session holds the reference to the object and does not have a serialization process involve.
    i agree with eric about the db close between operations.


    Alan Lavintman
     


    Alan Lavintman
  •  09-22-2006, 05:51 PM 29479 in reply to 29473

    Re: Problem with duplicating objects

    alan_lavintman:

    mmhh i think that session holds the reference to the object

    What object? After making a response there is no object on the server to hold reference to. Session variables exsist because of the stateless nature of the web. You can store (serialize) your object in a form that can make a roundtrip to the client and back (ie the hidden field on a page). This serializered form of your object has no reference to anything and nothing has reference to it. It's just data that can be deserialized back to an object later.

    Try storing a non-serializable object to a session variable.

    Goran
  •  09-22-2006, 09:29 PM 29488 in reply to 29479

    Re: Problem with duplicating objects

    I understand what you mean goran, i will have to make a use case holding a conection or an unserialized object on the session variable because i still think that session can hold references without serialize them, but any object that wants to live on a viewstate, yee,  offocurse, it has to implement serialize.

    Alan


    Alan Lavintman
  •  09-23-2006, 12:02 AM 29492 in reply to 29488

    Re: Problem with duplicating objects

    If you have an opbject which exists in db, you need to save this object(exactly the same object, not the object with same field values). I mean, if you save an object coming from container one should not be saved with container two. this will result in duplication

View as RSS news feed in XML