This discussion is archived
6 Replies Latest reply: Sep 18, 2008 11:10 PM by 807589 RSS

Design Problem: Object reference another object at different states

807589 Newbie
Currently Being Moderated
Hi,

This is a problem I came across today while developing a simple prototype. Imagine I have a class Boy which has a one-to-many relation with a class Dog.

The class Boy has the following methods: getName(), setName(), getAge(), setAge(), getDogs().

The class Dog has the following methods: getName(), getOwner()

When the application starts I load a Boy from the database as follows:

Boy peter = BoyFactory.loadBoyByName("Peter");
peter.setAge(10);

I then add peter to a static list which can be accessed from everywhere within my application.

At a different stage in the application I want to load a dog (just a dog). So I use the following code:

Dog dog = DogFactory.loadDogByOwnerName("Peter");

The dog object (using JDO or Hibernate) would also have a reference to the Boy object. However when calling the getOwner() method, the boy returned will not be the same boy stored in that static list (I expect the age, which was not persisted in the database, to be different).

Is there a way how when loading the Dog object, since its Owner of type Boy is already loaded in my object model, a Framework such as Hibernate or JDO will use this object rather then re-build a new one.

I am planning to use either Hibernate or some JDO framework and that is why I am asking this question.
If I where going to use some custom made way to load Dog then I would just search if the Owner was in that static list and if yes then set the owner of the dog equal to the object found in the static list.

However I was wondering if there is some design I should consider (study) to handle such a problem.

Thanks and Regards,
Sim085
  • 1. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    sim085 wrote:
    However I was wondering if there is some design I should consider (study) to handle such a problem.
    Shouldn't you just make sure the in-memory objects are sychronized with the on-file representation? If you call say,

    peter.setAge(10);

    then this call should update the object both in-memory and on-file.
  • 2. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    Yes that would partially solve the problem but the main problem would still be there. The application would have two object representing the same entity. If I change the age again and I submit that change to the database I still would have the object in the static list in an invalid state (since it would have the old age not the new one).

    Regards,
    Sim085
  • 3. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    Hibernate's own session management would likely take care of many of your worries
  • 4. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    Thanks for your reply. I guess the best way to know is by actually trying it. I was planning to use iBatis as a persistence framework so I will see if I can handle such a scenario with that. If not then I will see what other persistence frameworks have to offer.

    I mostly made this post to see if there was some design pattern actually aimed to overcome such a scenario. However it seems that the only way (unless the persistence framework already takes care of it) is to actually build the object yourself and then (in this case) check if an instance of the object reference by the loaded object already exists and if so use that object.

    Thanks again,
    Sim085
  • 5. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    sim085 wrote:
    I mostly made this post to see if there was some design pattern actually aimed to overcome such a scenario. However it seems that the only way (unless the persistence framework already takes care of it) is to actually build the object yourself and then (in this case) check if an instance of the object reference by the loaded object already exists and if so use that object.
    Well, you need to establish object identity. As you indicate the common implementation technique is by the way of an object pool (like in String).

    Java offers some help here with weak pointers. (See for example WeakHashMap). It makes sure the object pool won't grow out of bounds.
  • 6. Re: Design Problem: Object reference another object at different states
    807589 Newbie
    Currently Being Moderated
    Thanks uj :) I had another thought...

    I will give another scenario so things are more clear.

    Imagine having a class Teacher which has one-to-many relation with a class Student.

    If you where programming the classes for these two entities then would you program the student class as follows:
    class Student{
     Teacher t = null;
     void setTeacher(Teacher t){...}
     Teacher getTeacher(){...}
    }
    OR
    class Student{
     int teacherId = -1;
     void setTeacherId(int tId){...}
     int getTeacherId(){...}
    }
    The difference between these two methods is that in the first method, if I wanted to know the teacher of a student then I would only have to call the getTeacher() method while using the second design I would first need to get the teacher Id and then make a database call to actually get the Teacher object (or else get it from a static list where the teacher could be saved.

    Personally I prefer the first method since I like to have an object model where objects are directly linked with other rather then linked through some form of id. However I do not know which design is best to use...

    Thanks
    Sim085