Re: Working with Transient Objects

From: Elia Morling (eli..amiljen.se)
Date: Wed Jun 30 2004 - 12:33:44 EDT

  • Next message: Gentry, Michael: "RE: Working with Transient Objects"

    Sorry, let me try to make a better example.

    In the Cayenne Modeler I have a Car and a ParkingLot. In my
    database I have stored 1 ParkingLot and 5 Car(s).

    Now, I fire up my game. I get the ParkingLot and 5 Car(s) all stored
    in the database. Now I want 5 randomly created computer-controlled
    Car(s) for the duration of the game only. I want to use all the fields in
    my Car class as they are identical, except that I don't want to store
    them in the database. Why? Because the next time I start the game I
    want to randomly create 5 new ones. Meanwhile I want to commit any
    changes made to the 5 Car(s) controlled by players. All I need is
    a PersistenceState flag like NEW_DONT_COMMIT or similar on
    the computer controlled Car(s) so that the DataContext doesn't store
    them in the database.

    Elia

    ----- Original Message -----
    From: "Gentry, Michael" <michael_gentr..anniemae.com>
    To: "Elia Morling" <eli..amiljen.se>; <cayenne-user@objectstyle.org>
    Sent: Wednesday, June 30, 2004 6:05 PM
    Subject: RE: Working with Transient Objects

    I'm not sure if I understand what you are trying to do, but ...

    You have two entities which you modeled in Cayenne Modeler, Car and
    ParkingLot. You want to persist the ParkingLot entities, but not the
    Car entities? Do you ever persist Car entities? It doesn't sound to me
    like you want to persist them.

    If you do not want to persist the Car, you shouldn't be modeling it in
    Cayenne Modeler at all. What you should do is model the ParkingLot
    entity and in the subclass of the ParkingLot class (where you add your
    business logic), create your own setters, getters, and transient
    instance variables. Something similar to this:

    public class ParkingLot extends _ParkingLot
    {
      private List cars;

      public void addToCarArray(Car car)
      {
        if (cars == null)
          cars = new List();
        cars.add(car);
      }

      public void removeFromCarArray(Car car)
      {
        if (cars != null)
          cars.remove(car);
      }

      public List cars()
      {
        if (cars == null)
          cars = new List();

        return cars;
      }
    }

    NOTE: I didn't try to compile any of the above, but the general idea is
    to handle transient information in the subclass. You don't model
    transient information in Cayenne Modeler (the Car -- make plain old java
    class for it). It's also a good idea to use the subclass to handle
    derived information, possibly even caching it for efficiency (if it
    makes sense to do so -- you have to be careful if the parent values
    change, which would change the derived value). For example, you could
    have a fullName() method which returns firstName() + " " + middleName()
    + " " + lastName(). And, of course, any convenience methods go in the
    subclass, too.

    Hope that helps!

    /dev/mrg

    -----Original Message-----
    From: Elia Morling [mailto:eli..amiljen.se]
    Sent: Tuesday, June 29, 2004 5:21 PM
    To: cayenne-use..bjectstyle.org
    Subject: Working with Transient Objects

    Hi,
    Here's an example to illustrate my problem. I have a parking lot
    with 5 cars stored in the database. Every time I start up my
    application I need to create 5 new cars for the duration of the
    run only. I don't want to store them in the database so I simply
    create the objects without a context using regular new().

    Now I can't make them work with the parking lot, when using:

    parkinglot.addToCarArray(car);

    OR

    car.setParkinglot(parkinglot)

    Why doesn't this work and what should I do about it? I need to
    have database cars and non-database cars to co-exist in the
    parkinglot.

    Elia



    This archive was generated by hypermail 2.0.0 : Wed Jun 30 2004 - 12:33:47 EDT