You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Adam Yocum <ad...@yahoo.com> on 2007/12/22 19:30:29 UTC

Setting Default Values for Entities

Hi,
   
  Can anyone suggest how I might get this simple query to return me results using Cayenne and ROP.  I am using 3.0M2, and I need something like this MySQL metadata query... 
   
  'SHOW columns FROM tablename'
   
  The fields returned are 'Field', 'Type', 'Null', 'Key', 'Default', and 'Extra'
   
  I want to loop through the results and set the entity property for each 'Field' to 'Default', except the PK of course.
   
  Our database spec has no NULLs in the entire DB, every field is NOT NULL with a DEFAULT value that varies depending on what type of field and other business logic.
   
  If there is some way to do this using Cayenne's metadata abilities I'm all ears!
   
  Thanks,
  Adam

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

Re: Setting Default Values for Entities

Posted by Adam Yocum <ad...@yahoo.com>.
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 <an...@objectstyle.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.

Re: Setting Default Values for Entities

Posted by Adam Yocum <ad...@yahoo.com>.
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 <an...@objectstyle.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.

Re: Setting Default Values for Entities

Posted by Andrus Adamchik <an...@objectstyle.org>.
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
>
>
>


Re: Setting Default Values for Entities

Posted by Adam Yocum <ad...@yahoo.com>.
I thought I'd go ahead and post the Listener that I created to handle setting up the defaults for a MySQL DB dynamically.  The listener gets the default values from the MySQL DB and then loops through the attributes of the entity object setting the defaults pulled down from the server.
   
  This of course does only work when using the CayenneDataObject, not the PersistentObject that the client classes use, but I figured it might be useful for somebody.
   
  package test;
  /**
 *
 * @author Adam
 */
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.cayenne.CayenneDataObject;
import org.apache.cayenne.LifecycleListener;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.query.SQLTemplate;
  public class MyListener implements LifecycleListener {
      public void prePersist(Object entity) {
        setNewObjectDefaults((CayenneDataObject)entity);
    }
      private void setNewObjectDefaults(CayenneDataObject CayenneObj) {
        ObjEntity ent = CayenneObj.getObjEntity();
        Collection attribs = ent.getAttributes();
          String defaultSql = "SHOW columns FROM "+ent.getDbEntityName();
        SQLTemplate rawSelect = new SQLTemplate(CayenneObj.getClass(), defaultSql);
        rawSelect.setFetchingDataRows(true);
        List DefaultList = CayenneObj.getObjectContext().performQuery(rawSelect);
        
        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);
              Object Default = null;
            for(int j=0;j<=DefaultList.size()-1;j++)
            {
                Map row = (Map)DefaultList.get(j);
                String rowFieldName = (String)row.get("Field");
                if(rowFieldName.equals(att.getName()))
                    Default = (Object)row.get("Default");
            }
              if (propertyValue == null) 
            {
                CayenneObj.writeProperty(propName, Default);
            } 
        }
    }
      public void postPersist(Object arg0) {
      }
      public void preRemove(Object arg0) {
      }
      public void postRemove(Object arg0) {
      }
      public void preUpdate(Object arg0) {
      }
      public void postUpdate(Object arg0) {
      }
      public void postLoad(Object arg0) {
      }
}


Andrus Adamchik <an...@objectstyle.org> wrote:
  
On Dec 23, 2007, at 6:05 AM, Aristedes Maniatis wrote:

>>
>> Since the method for setting defaults in the db is the same accross 
>> the database depending only on field type I can put together 
>> something like the following to initialize objects following our 
>> rules... still it does not work ROP :( PersistentObject used for 
>> client seems to have no way to access the metadata like 
>> CayenneDataObject does, i.e. no getObjEntity()
>
> Yeah, anything you do with this will need to be server side, not in 
> the client. But lifecycle events work only on the server anyway, and 
> only once you've committed the object from the client. The code you 
> posted could go into the common superclass for your persistent 
> objects on the server. Catch the lifecycle event (either preUpdate 
> or prePersist) and fire off your code.
>
> http://cayenne.apache.org/doc/lifecycle-callbacks.html

Yep, looks like defaults extraction can be done in a generic fashion, 
and be abstracted in a single listener (or persistent superclass). I 
prefer listener, as it wouldn't have to store shared state in static 
variables. BTW, this could be a nice addition to Cayenne core down the 
line (we can support runtime defaults extraction via DbAdapter).

On the ROP end for now invalidation with RefreshQuery maybe the only 
way to get the defaults from the server, but eventually we'd like to 
implement a smarter protocol that would pass any custom changes made 
via callbacks and listeners on the server back to the client.

Andrus



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

Re: Setting Default Values for Entities

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Dec 23, 2007, at 6:05 AM, Aristedes Maniatis wrote:

>>
>> Since the method for setting defaults in the db is the same accross  
>> the database depending only on field type I can put together  
>> something like the following to initialize objects following our  
>> rules...  still it does not work ROP :(   PersistentObject used for  
>> client seems to have no way to access the metadata like  
>> CayenneDataObject does, i.e. no getObjEntity()
>
> Yeah, anything you do with this will need to be server side, not in  
> the client. But lifecycle events work only on the server anyway, and  
> only once you've committed the object from the client. The code you  
> posted could go into the common superclass for your persistent  
> objects on the server. Catch the lifecycle event (either preUpdate  
> or prePersist) and fire off your code.
>
> http://cayenne.apache.org/doc/lifecycle-callbacks.html

Yep, looks like defaults extraction can be done in a generic fashion,  
and be abstracted in a single listener (or persistent superclass). I  
prefer listener, as it wouldn't have to store shared state in static  
variables. BTW, this could be a nice addition to Cayenne core down the  
line (we can support runtime defaults extraction via DbAdapter).

On the ROP end for now invalidation with RefreshQuery maybe the only  
way to get the defaults from the server, but eventually we'd like to  
implement a smarter protocol that would pass any custom changes made  
via callbacks and listeners on the server back to the client.

Andrus


Re: Setting Default Values for Entities

Posted by Aristedes Maniatis <ar...@ish.com.au>.
On 23/12/2007, at 2:10 PM, Adam Yocum wrote:

> Hi Ari,
>
>  How can I execute an arbitrary query like the SHOW query to get the  
> map of property names and default values, I like that idea, I just  
> don't know how to get a plain query to execute with just a client  
> app.  The client has not db driver lib.  Can I use Cayenne to  
> execute an arbitrary query?

http://cayenne.apache.org/doc/sqltemplate-query.html

But if you can hardcode the choices of default values to just a few  
options (such as in the code you posted), then you will not need this.


>  Since the method for setting defaults in the db is the same accross  
> the database depending only on field type I can put together  
> something like the following to initialize objects following our  
> rules...  still it does not work ROP :(   PersistentObject used for  
> client seems to have no way to access the metadata like  
> CayenneDataObject does, i.e. no getObjEntity()

Yeah, anything you do with this will need to be server side, not in  
the client. But lifecycle events work only on the server anyway, and  
only once you've committed the object from the client. The code you  
posted could go into the common superclass for your persistent objects  
on the server. Catch the lifecycle event (either preUpdate or  
prePersist) and fire off your code.

http://cayenne.apache.org/doc/lifecycle-callbacks.html

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



Re: Setting Default Values for Entities

Posted by Adam Yocum <ad...@yahoo.com>.
Hi Ari,
   
  How can I execute an arbitrary query like the SHOW query to get the map of property names and default values, I like that idea, I just don't know how to get a plain query to execute with just a client app.  The client has not db driver lib.  Can I use Cayenne to execute an arbitrary query?  
   
  Since the method for setting defaults in the db is the same accross the database depending only on field type I can put together something like the following to initialize objects following our rules...  still it does not work ROP :(   PersistentObject used for client seems to have no way to access the metadata like CayenneDataObject does, i.e. no getObjEntity()
   
  If I can get that SHOW query going with ROP then I can set up the objects with the actual defaults in the prePersist() and that would be great!
   
  Here is a method I am using to automatically set up CayenneDataObjects with company database compliant defaults based on their database type....
   
  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);
          
          System.out.println("att type = "+TypesMapping.getSqlNameByType(att.getType()));
          String type = TypesMapping.getSqlNameByType(att.getType());
          
          if(propertyValue==null)
          {
              if(type.equals(TypesMapping.SQL_INTEGER))
              {
                  CayenneObj.writeProperty(propName, 0);
              }
              else if(type.equals(TypesMapping.SQL_DOUBLE))
              {
                  CayenneObj.writeProperty(propName, 0.0000);
              }
              else if(type.equals(TypesMapping.SQL_TINYINT))
              {
                  CayenneObj.writeProperty(propName, 0);
              }
              else if(type.equals(TypesMapping.SQL_VARCHAR) || type.equals(TypesMapping.SQL_LONGVARCHAR) || type.equals(TypesMapping.SQL_CHAR))
              {
                  CayenneObj.writeProperty(propName, "");
              }
          }
          else
          {
              System.out.println("Attribute "+propName+" IS "+propertyValue.toString());
          }
      }
  }

  Thanks for all your help.  The company I am doing all this for is a T shirt company, I'd love to make a couple of Cayenne t shirts and send them out to you guys Ari, and Andrus for helping me out.  If you are interested email me on my private email.
   
  I new to this ORM, so I promise to ask less dumb questions as time goes on!  
   
  Thanks,
  Adam
  
Aristedes Maniatis <ar...@ish.com.au> wrote:
  I don't think it can be done. As Andrus was suggesting, JDBC doesn't 
seem to have a way to read those values from the database schema. The 
only portable approach I can think of is using SQLtemplate to create a 
dummy object at system startup, read back its values and then delete 
that record.

It seems like an unusual request though - can you not simply implement 
the default values within Cayenne rather than within the database? Or 
else use the mysql command "SHOW CREATE TABLE" to get the default 
values and map them onto the right attributes at startup. Then you can 
populate the values in prePersist() or wherever is appropriate as each 
record is created.

Ari Maniatis



On 23/12/2007, at 10:12 AM, Adam Yocum wrote:

> Hi Ari,
>
> It looks like I can get close with ...
>
> http://cayenne.apache.org/doc/api/org/apache/cayenne/map/DbAttribute.html#getType()
>
> That returns the SQL type that of the DbAttribute, if only I could 
> get the Default Value set in the database.
>
> I need something like DbAttribute.getDefaultValue();
>
> suggestions?
>
> Thanks,
> Adam
>
> 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
>
>
>
>
>
> ---------------------------------
> Looking for last minute shopping deals? Find them fast with Yahoo! 
> Search.





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




       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

Re: Setting Default Values for Entities

Posted by Aristedes Maniatis <ar...@ish.com.au>.
I don't think it can be done. As Andrus was suggesting, JDBC doesn't  
seem to have a way to read those values from the database schema. The  
only portable approach I can think of is using SQLtemplate to create a  
dummy object at system startup, read back its values and then delete  
that record.

It seems like an unusual request though - can you not simply implement  
the default values within Cayenne rather than within the database? Or  
else use the mysql command "SHOW CREATE TABLE" to get the default  
values and map them onto the right attributes at startup. Then you can  
populate the values in prePersist() or wherever is appropriate as each  
record is created.

Ari Maniatis



On 23/12/2007, at 10:12 AM, Adam Yocum wrote:

> Hi Ari,
>
>  It looks like I can get close with ...
>
>  http://cayenne.apache.org/doc/api/org/apache/cayenne/map/DbAttribute.html#getType()
>
>  That returns the SQL type that of the DbAttribute, if only I could  
> get the Default Value set in the database.
>
>  I need something like DbAttribute.getDefaultValue();
>
>  suggestions?
>
>  Thanks,
>  Adam
>
> Aristedes Maniatis <ar...@ish.com.au> 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
>
>
>
>
>
> ---------------------------------
> Looking for last minute shopping deals?  Find them fast with Yahoo!  
> Search.





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



Re: Setting Default Values for Entities

Posted by Adam Yocum <ad...@yahoo.com>.
Hi Ari,
   
  It looks like I can get close with ...
   
  http://cayenne.apache.org/doc/api/org/apache/cayenne/map/DbAttribute.html#getType()
   
  That returns the SQL type that of the DbAttribute, if only I could get the Default Value set in the database.
   
  I need something like DbAttribute.getDefaultValue();
   
  suggestions?
   
  Thanks,
  Adam

Aristedes Maniatis <ar...@ish.com.au> 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




       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

Re: Setting Default Values for Entities

Posted by Aristedes Maniatis <ar...@ish.com.au>.
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