Re: Setting Default Values for Entities

From: Adam Yocum (adamyocu..ahoo.com)
Date: Sat Dec 22 2007 - 20:45:44 EST

  • Next message: Aristedes Maniatis: "Re: Setting Default Values for Entities"

    Hi Everybody,
       
      I have managed to produce the behavior I was looking for, but only with CayenneDataObjects, not PersistentObjects. Only working for the server classes, not for my client. Is there anyway to do something like the following code example in an ROP app? or am I going to have to figure something else out?
       
      The following code basically loops through a CayenneDataObject and sets all the fields to
    Generated that are NULL, that makes Cayenne not enter them in the INSERT statement and thus leaving the DB to substitute the Default value for the missing fields in the insert statement. This is a big time hack and since I don't know if i can even get it to work with the client objects I'm very skeptical this is the right way to go.
       
      As you can see I have to commit after each object because it changes the way the entire class handles it's fields instead of each individual CayenneDataObject instance.
       
      Sorry for such a long post, but this is some hack.
       
      OK, here's the hack example, replace the OrderWebDesign object with whatever you got and watch it go, or just check out my output...
       
      Notice that the first query omits all the fields, and the second does not.
      Here is the output I get...
       
       
      Attribute designDescription IS NULL
    Setting to Generated...
    Attribute designDimension IS NULL
    Setting to Generated...
    Attribute designDimensionType IS NULL
    Setting to Generated...
    Attribute designLinkId IS NULL
    Setting to Generated...
    Attribute designLinkType IS NULL
    Setting to Generated...
    Attribute designLocation IS NULL
    Setting to Generated...
    Attribute designName IS NULL
    Setting to Generated...
    Attribute designNotes IS NULL
    Setting to Generated...
    Attribute designNumColors IS NULL
    Setting to Generated...
    Attribute projectId IS NULL
    Setting to Generated...
    cayenne INFO [main 12-22 20:39:28] QueryLogger: Opening connection: jdbc:mysql://mydomain.com/orderdb
            Login: xxxx
              Password: *******
    cayenne INFO [main 12-22 20:39:30] QueryLogger: +++ Connecting: SUCCESS.
    cayenne INFO [main 12-22 20:39:30] QueryLogger: --- transaction started.
    cayenne DEBUG [main 12-22 20:39:30] ResourceLocator: URL found with classloader: jar:file:/C:/SLall/adam/LizardWizard/lib/cayenne-server-3.0M2.jar!/org/apache/cayenne/dba/mysql/types.xml
    cayenne INFO [main 12-22 20:39:30] QueryLogger: Detected and installed adapter: org.apache.cayenne.dba.mysql.MySQLAdapter
    cayenne INFO [main 12-22 20:39:30] QueryLogger: --- will run 1 query.
    cayenne INFO [main 12-22 20:39:30] QueryLogger: INSERT INTO order_web_design () VALUES ()
    cayenne INFO [main 12-22 20:39:30] QueryLogger: === updated 1 row.
    cayenne INFO [main 12-22 20:39:30] QueryLogger: +++ transaction committed.
    Attribute designDescription IS asfdsa
    Attribute designDimension IS asdfsa
    Attribute designDimensionType IS gfags
    Attribute designLinkId IS 3430
    Attribute designLinkType IS sdfsa
    Attribute designLocation IS sdfsfds
    Attribute designName IS sdfdsfsd
    Attribute designNotes IS sdfdsfsdf
    Attribute designNumColors IS 343
    Attribute projectId IS 3340
    cayenne INFO [main 12-22 20:39:30] QueryLogger: --- will run 1 query.
    cayenne INFO [main 12-22 20:39:30] QueryLogger: --- transaction started.
    cayenne INFO [main 12-22 20:39:30] QueryLogger: INSERT INTO order_web_design (design_description, design_dimension, design_dimension_type, design_link_id, design_link_type, design_location, design_name, design_notes, design_num_colors, project_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    cayenne INFO [main 12-22 20:39:30] QueryLogger: [bind: 'asfdsa', 'asdfsa', 'gfags', 3430, 'sdfsa', 'sdfsfds', 'sdfdsfsd', 'sdfdsfsdf', 343, 3340]
    cayenne INFO [main 12-22 20:39:30] QueryLogger: === updated 1 row.
    cayenne INFO [main 12-22 20:39:31] QueryLogger: +++ transaction committed.

      HERE IS THE CODE...
       
      package test;
       
      import java.util.Collection;
    import org.apache.cayenne.CayenneDataObject;
    import org.apache.cayenne.CayenneDataObject;
    import org.apache.cayenne.ObjectContext;
    import org.apache.cayenne.access.DataContext;
    import org.apache.cayenne.map.DbAttribute;
    import org.apache.cayenne.map.ObjAttribute;
    import org.apache.cayenne.map.ObjEntity;
    import org.apache.log4j.Logger;
    import org.spottedlizard.cayenneAPI.order.OrderWebDesign;
       
      public class MainServerTest
    {
      private static final Logger logger = Logger.getLogger(MainServerTest.class);
        // Provide a Cayenne context.
      private ObjectContext context;
        public static void main(String[] args)
      {
        MainServerTest main = new MainServerTest();
        main.runExample();
        System.exit(0);
      }
      
      public void runExample()
      {
          OrderWebDesign design = (OrderWebDesign)context.newObject(OrderWebDesign.class);
            setNewObjectDefaults(design);
          context.commitChanges();
          
          OrderWebDesign design2 = (OrderWebDesign)context.newObject(OrderWebDesign.class);
          design2.setDesignDescription("asfdsa");
          design2.setDesignDimension("asdfsa");
          design2.setDesignDimensionType("gfags");
          design2.setDesignLinkId(3430);
          design2.setDesignLinkType("sdfsa");
          design2.setDesignLocation("sdfsfds");
          design2.setDesignName("sdfdsfsd");
          design2.setDesignNotes("sdfdsfsdf");
          design2.setDesignNumColors(343);
          design2.setProjectId(3340);
          
          setNewObjectDefaults(design2);
          context.commitChanges();
      }
      
      private void setNewObjectDefaults(CayenneDataObject CayenneObj)
      {
          ObjEntity ent = CayenneObj.getObjEntity();
          Collection attribs = ent.getAttributes();
          
          for(int i=0;i<=attribs.size()-1;i++)
          {
              ObjAttribute ObjAtt = (ObjAttribute)attribs.toArray()[i];
              
              DbAttribute att = ObjAtt.getDbAttribute();
              
              String propName = ObjAtt.getName();
              
              Object propertyValue = CayenneObj.readProperty(propName);
              
              if(propertyValue==null)
              {
                  System.out.println("Attribute "+propName+" IS NULL");
                  System.out.println("Setting to Generated...");
                  //SET TO GENERATED SO THAT CAYENNE LETS THE DB DECIDE
                  att.setGenerated(true);
              }
              else
              {
                  System.out.println("Attribute "+propName+" IS "+propertyValue.toString());
                  //TURN BACK ON TO GENERATED SO THAT CAYENNE INSERTS THE FIELD
                  att.setGenerated(false);
              }
          }
      }
        
      public MainServerTest()
      {
        super();
        context = DataContext.createDataContext();
      }
    }

       
       
       
       
      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
    >
    >
    >

           
    ---------------------------------
    Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.



    This archive was generated by hypermail 2.0.0 : Sat Dec 22 2007 - 20:46:21 EST