Re: Setting Default Values for Entities

From: Adam Yocum (adamyocu..ahoo.com)
Date: Sat Dec 22 2007 - 18:00:54 EST

  • Next message: Adam Yocum: "Re: Setting Default Values for Entities"

    Hi Andrus,
       
      My problem is stemming from not being able to get the defaults set in the first place. When I make a new object and do not set any of the objects properties I get an insert something like this going on....
       
      INSERT INTO tablename(property1, property2, property3) VALUES(NULL, NULL, NULL)
       
      of course Cayenne uses 'bind'.
       
      what I would like is for the insert to be like this...
       
      INSERT INTO tablename() VALUES()
       
      or this also would work...
       
      INSERT INTO tablename(property1,property2,property3) VALUES(DEFAULT, DEFAULT, DEFAULT)
       
      DEFAULT is a key word in MySQL that sets the field to the columns default value.
       
      So, I am basically trying to figure out how to have Cayenne either substitute DEFAULT for all NULLs, omit the property on INSERT if it is NULL, or my plan for a work around was to retrieve the default value metadata from the MySQL server and then set all the fields to their default values using a util method like this ( this is mostly pseudocode )
       
      public DataObject SetDefaultsForDataObject(DataObject obj)
      {
           Results = Execute("SHOW columns FROM obj.tablename");
       
           while(Results.next())
           {
                 field = Results.get("Field");
                 default = Results.get("Default");
                 obj.setProperty(field,default);
           }
       
           return obj;
      }
       
      Is there anyway to not put the properties in the INSERT statement if they are NULL, that is how MySQL knows to do the default unless you specify DEFAULT keyword.
       
      Any additional help on this would be very appreciated!
       
      Thanks again,
      Adam

    Andrus Adamchik <andru..bjectstyle.org> wrote:
      Syncing DB defaults with entities is indeed a painful exercise as JDBC
    provides no callback mechanism to let the app know which defaults were
    set. Here is one more brute force mechanism to achieve that -
    invalidate all newly inserted objects. E.g., using 3.0 API:

    List newObjects = context.newObjects();
    context.commitChanges();
    context.performGenericQuery(new RefreshQuery(newObjects));

    Andrus

    On Dec 22, 2007, at 11:57 PM, Aristedes Maniatis wrote:

    >
    > On 23/12/2007, at 5:30 AM, Adam Yocum wrote:
    >
    >> 'SHOW columns FROM tablename'
    >
    > This might be a good starting point:
    >
    > http://cayenne.apache.org/doc/api/org/apache/cayenne/map/ObjEntity.html#getAttributes()
    >
    > Ari Maniatis
    >
    >
    >
    > -------------------------->
    > ish
    > http://www.ish.com.au
    > Level 1, 30 Wilson Street Newtown 2042 Australia
    > phone +61 2 9550 5001 fax +61 2 9550 4001
    > GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A
    >
    >
    >

           
    ---------------------------------
    Never miss a thing. Make Yahoo your homepage.



    This archive was generated by hypermail 2.0.0 : Sat Dec 22 2007 - 18:01:32 EST