Re: Synch objEntity and dbEntity

From: Jason Dwyer (jason..edata.com.au)
Date: Thu Oct 06 2005 - 22:12:21 EDT

  • Next message: jira-norepl..bjectstyle.org: "[OS-JIRA] Created: (CAY-389) WebSphere and Commons Logging"

    Hi John,

    your best bet will be to not expose primary keys in the model. ORM using
    cayenne strives toward presenting an abstracted view of the underlying
    data that hides the implementation of relationships from the api.

    if youre working with a clean slate: that is you're building the db and
    app at the same time, you should strive to arrange your data such that the
    primary keys are meaningless, typically a serial integer value.

    there are times in the application where you will want to reference
    objects based on the details of the primary key, and for these, its a
    simple matter of adding a method to your object entity subclass which gets
    the primary key value from the internal data for the object matching the
    row. something like this:

    pubic class MyObject extends _MyObject {// an extension of CayenneDataObject
        ...

        /** convenience method to expose the underlying primary key */
        public int getPrimaryKey(){

            if(getObjectId()!=null){
                return DataObjectUtils.intPKForObject(this);
            }
            return -1;
        }

    }

    you can also set up static factory methods which wrap around the
    DataObjectUtils functionality to retrieve objects based on row primary
    keys:

        public static MyObject getMyObjectByRowID(DataContext dc, int rowID){

           return (MyObject)DataObjectUtils.objectForPK(
                   dc, MyObject.class, rowID);
        }

    in this paradigm, you never want to actually set the primary key in your
    data objects directly: rely on the framework to set that up. you instead
    concern yourself with the Object relationships, and let cayenne manage how
    that will be persisted to the underlying db.

    if youre stuck on a legacy database with meaningful PK's, you might need
    to come up with a different strategy. DataObjectUtils is packed with
    helpful stuff, i suggest you start from there.

    hope this helps,

    cheers,

    jd

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA256
    >
    > To all,
    >
    > I think that this is a bug in the modeler, but I will toss it to the
    > group first.
    >
    > So I have a primary key, that I would like to expose in the
    > objEntity, if I add it, and then do some other work like make
    > relationships. And then click the objEntity synch button the primary
    > key disappears.
    >
    > Same thing hapens if I expose some other Ids, and then synch they
    > disappear also.
    >
    > It seems to me that if I decide to expose something from the DbEntity
    > into the objEntity it should remain that way.
    >
    > Or am I missing something.
    >
    > - -John
    >
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: PGP Desktop 9.0.1 (Build 2185)
    >
    > iQEVAwUBQ0WrlqF6YHs+0wZ7AQgiMAf+P0UYrR/qrP8GhQ6CtZ58W4zSsBL8VSEG
    > bVgD3YOtOxl0zG3Wg5hax9VsEBCbER3fEalRHvsOW0sXqTjDyed7ZPtaB9G6+lKS
    > rAc6502/98nOzTrQ5vyzHXaQtJdF3DMYd/R51C07sHtlfoXmJq/Obxl5y81qG3Zu
    > FjMnukO0Jz5/Wv79ahSrkUCzN5VnE4gZOD2EhUy50aHY0cahg+0uAFZUgTec7KXT
    > qG8YKbm+m36UHuQgJjUG7jpJuyA2ORJkMG0885Ps8JTqH1fsXDa1D4Sdq4pIh/S3
    > uqLrlVhl9Uej6OyblYT8mR++DKkhhOGSWVVe191oTKSKmtKkJ/qiDQ==
    > =uorR
    > -----END PGP SIGNATURE-----
    >



    This archive was generated by hypermail 2.0.0 : Thu Oct 06 2005 - 22:12:24 EDT