Return-Path: Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: (qmail 11864 invoked from network); 23 Dec 2007 03:11:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Dec 2007 03:11:32 -0000 Received: (qmail 23593 invoked by uid 500); 23 Dec 2007 03:11:21 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 23581 invoked by uid 500); 23 Dec 2007 03:11:21 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 23572 invoked by uid 99); 23 Dec 2007 03:11:21 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Dec 2007 19:11:21 -0800 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [206.190.48.235] (HELO web52712.mail.re2.yahoo.com) (206.190.48.235) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 23 Dec 2007 03:10:56 +0000 Received: (qmail 26903 invoked by uid 60001); 23 Dec 2007 03:10:59 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=NdeBgy3su6Gv4TPyCatCwugNMO8aUaOhMr1GST1D9JUNoT6dHfks5WdWg9q29PI7yhoh2jlVK/dbtIdeFXJzTOCAvAQTnaq22rgDfKJh5gm2kYSv4gNgua3v+QhBVz0Rrbw3zQghz7bjd71i/Mjv08G/wGB3yyglzIAdNeIsL3I=; X-YMail-OSG: AyI5mhMVM1mOyr.u5plnPdT5hyyu1_5FtwdkDpxaxjhLoloWzQdIKmth4tB24gpQB5fCOl.3hmZFL9s2y16f_7NhB7sfWocna8IefgpzLNqBWQphLq8I8686zRPrFQ-- Received: from [71.240.196.202] by web52712.mail.re2.yahoo.com via HTTP; Sat, 22 Dec 2007 19:10:58 PST Date: Sat, 22 Dec 2007 19:10:58 -0800 (PST) From: Adam Yocum Subject: Re: Setting Default Values for Entities To: user@cayenne.apache.org In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-541304217-1198379458=:26811" Content-Transfer-Encoding: 8bit Message-ID: <89716.26811.qm@web52712.mail.re2.yahoo.com> X-Virus-Checked: Checked by ClamAV on apache.org --0-541304217-1198379458=:26811 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit 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 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. --0-541304217-1198379458=:26811--