Re: getPrimaryKeys ?

From: Ryan Holmes (rya..yperstep.com)
Date: Fri Feb 03 2006 - 04:08:01 EST

  • Next message: Joshua Pyle: "Re: getPrimaryKeys ?"

    I don't think there is an easy way to do what you want. I'm sure you
    could get a handle to the ObjEntity for the current class, then iterate
    through its DbAttributes, check isPrimaryKey() and ultimately read the
    primary key properties but I wouldn't recommend coupling your classes so
    tightly to an ORM (not to mention the potential performance issues).

    There are also a lot of good reasons not to rely on a superclass's
    equals() method in the first place. My advice is to stick to the
    principals of good class design and just implement equals() in your classes.

    Hope that helps,
    -Ryan

    Arnaud Garcia wrote:
    > hi ryan,
    > you right, my PK are in the XML and I just need to compare the
    > properties ...
    > But, my second question was: Is there a method to return all the PK
    > atributes of an object ? (of course I can look at my model, but I
    > would like to create a general method ....)
    >
    > for example:
    > o1 -
    > (String) field1 : PK
    > (Integer) field 2: PK
    > (String) field3
    > (String) field4
    >
    > something general, like o1.getPrimarykeyAttributes() which will return
    > field1 and field2
    >
    > thanks
    > arnaud
    >
    >
    > Ryan Holmes a écrit :
    >
    >> I'm confused - in your code snippet, o2.getObjectId() *should* return
    >> null because o2 is a new object and the PK value is unknown at that
    >> point. What kind of PK's are you working with:
    >> auto-increment/sequence, composite, natural or what?
    >>
    >> You mentioned that your data is coming from XML. Are the primary key
    >> values already assigned in the XML?
    >>
    >> My guess is that you simply need to expose your primary key value(s)
    >> as regular properties and use their getters in your equals() method,
    >> but I may not understand the situation.
    >>
    >> See http://objectstyle.org/confluence/display/CAY/Mapping+Primary+Keys
    >>
    >> -Ryan
    >>
    >> Arnaud Garcia wrote:
    >>
    >>> thanks Andrus,
    >>>
    >>> Well, I can't do this....
    >>> if o1 is in a dataContext, for example o1 is a child of O.
    >>> if I build o2 from nothing, o2 = new o2(), o2.set ... ect..
    >>> then if I do:
    >>> system.out.println(o1.getObjectid()), I will have all the values of o1
    >>> BUT
    >>> a println of o2.getObjectid() return null !
    >>>
    >>> So to solve my problem, I did a simple fields comparison between
    >>> the PK attributes of o1 and o2 and it works ... but I thought there
    >>> was another way.
    >>>
    >>> thanks
    >>> arnaud
    >>>
    >>>
    >>> Andrus Adamchik a écrit :
    >>>
    >>>> getPrimaryKeys() in Cayenne is getObjectId()... So you can do
    >>>> "o1.getObjectid().equals(o2.getObjectId())" comparison.
    >>>>
    >>>> Andrus
    >>>>
    >>>>
    >>>> On Jan 31, 2006, at 12:25 PM, Arnaud Garcia wrote:
    >>>>
    >>>>> Hello,
    >>>>>
    >>>>> Is there a way to get all the primary keys attributes of a
    >>>>> CayenneDataObject to compare it with another one?
    >>>>>
    >>>>> What I need to do:
    >>>>> I have another xml datasource which sends some objects, and I
    >>>>> need to compare these objects before adding them to another
    >>>>> object (one to many relationship) ... so I need to compare them
    >>>>> with their primary keys.
    >>>>>
    >>>>> if
    >>>>> anObject.getPrimaryKeys().equal(maybeTheSameObject.getPrimaryKeys()
    >>>>> then
    >>>>> // do nothing
    >>>>> else
    >>>>> containerObject.addTo(anObject)
    >>>>>
    >>>>> => If I don't test the object before adding it, I have a
    >>>>> violation constrainst ...
    >>>>>
    >>>>> thanks for help
    >>>>>
    >>>>> arnaud
    >>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Fri Feb 03 2006 - 04:08:10 EST