cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Yocum <adamyo...@yahoo.com>
Subject Re: Setting Default Values for Entities
Date Sun, 23 Dec 2007 03:10:58 GMT
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 <ari@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.
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message