You can use reflection.

Example:
     ...
     HashMap attrs = new HashMap();
     attrs.put( "attribute_1", val_1 );
     attrs.put( "attribute_2", val_2 );
     ...
     tx.begin();
     tx.lock(obj, tx.WRITE);
     Helper.populate(obj, attrs);
     tx.commit();
     ...

Helper:
    public static Object populate( Object o, HashMap attributes ) 
        throws Exception
    {
        if ( o == null || attributes == null ) { return o; }
        
        Iterator keys = attributes.keySet().iterator();
        while ( keys.hasNext() )
        {
            String key = (String)keys.next();
            try
            {
                String methodName = "set" + initialCaps( key );
                if ( attributes.get(key) == null )
                {
                    throw new Exception("value for key NULL");
                }
                Object[] argValue = { attributes.get(key) };
                Class[] paramTypes = { attributes.get(key).getClass() };
                
                java.lang.reflect.Method method = o.getClass().getMethod(methodName, 
paramTypes);
                method.invoke(o, argValue);
            }
            catch ( Exception e )
            {
                //log exception
                throw e;
            }
        }
        return o;
    }

Shannon Hardt

-----Original Message-----
From: Kevin Viet [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, November 27, 2002 11:08 AM
To: OJB Users List
Subject: design question


Hello all

ODMG specify that an object must be updated in between transaction
bounds

try {
  tx.begin()
  obj.setA(a);
  obj.setB(b);
  // ...
  tx.commit()
}
catch (Throwable t) {
  tx.rollback();
}

First question : if an error occurs after obj.setB() will the object
remains in an unaffected state ?

Second question : 
I can't manage to design a generic method to update my object with ODMG,
in effect with the persistence API, the only things to do is to call
update(object) on the broker API and the broker update the database.
With ODMG I have to create methods for all model objects and to put all
properties as arguments to these method in order to do in my method :

public void updateObjectOfClassA(A a, String prop1, String prop2,
String  prop3, .... )
{ 
  .....
  tx.begin()
  a.setProp1(prop1);
  a.setProp2(prop2); 
  .... 
  a.setPropN()
}

A hint or a source code could be of valuable help

-- 
Kevin Viet <[EMAIL PROTECTED]>
ActiVia Networks




--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to