How to read ObjectId value [Was: objectId seems not populated when the pk=objectId and mapped in the modeler]

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Jun 30 2003 - 11:00:51 EDT

  • Next message: Laszlo Spoor: "Re: objectId seems not populated when the pk=objectId and mapped in the modeler"

    A general tip to anybody who needs "read" access to object id value, but
    still wants it to be autogenerated. Having "write" access is EVIL in most
    cases - if you want to change a primary key, it is much cleaner to do
    "delete old/insert new" instead of modifying the key!

    Anyway... In subclass of your persistent object implement a simple method
    like this (similar to what Laszlo was doing in the earlier code example):

      public class MyObject extends _MyObject {
        ....
        public Number getId() {
          return (Number)getObjectId().getValueForAttribute("ID_COLUMN_NAME");
        }
      }

    Andrus

    > Hi Laszlo,
    >
    > The only way I can make this code to fail with "id" being the class
    > property, is when id value is not explicitly assigned in the code.
    > Cayenne assumes that if a PK column is a part of the class, then the
    > user is responsible for setting it. So no automatic PK generation is
    > done. When I do something like "fto.setId(new Integer(123))", everything
    > works
    > (including printout of the id value, and deleteObject).
    >
    > This is strange actually, cause the behavior in my environment is so
    > different from yours. For instance, the failure that you've described
    > never happens in my test app. Rather the code may fail much earlier on
    > insert - since PK is null.
    >
    > Is it possible that you have some unusual mapping for this entity? Are
    > there any Modeler validation warnings when you save the model?
    >
    > Andrus
    >



    This archive was generated by hypermail 2.0.0 : Mon Jun 30 2003 - 11:00:51 EDT