cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Yocum <>
Subject Re: Setting Default Values for Entities
Date Fri, 28 Dec 2007 16:36:26 GMT
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
  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.query.SQLTemplate;
  public class MyListener implements LifecycleListener {
      public void prePersist(Object 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);
        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");
                    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 <> 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.

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.


Never miss a thing.   Make Yahoo your homepage.
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message