from an OJB standpoint it looks OK.
If you want to use the interface IPersistenceService as an abstraction of the concrete persistence layer I have some doubts.
The Methods
public Object getObjectByCriteria(Class clazz, Criteria criteria) and
public Collection getCollectionByCriteria(Class clazz, Criteria criteria)
both have a Criteria parameter. Criteria is part of the OJB query package and so your interface contains a dependency on OJB. If you choose to work exclusively with OJB this is OK, but if you want to plugin different O/R layers you'll have to translate OJB Criteria to the query API of the other tool!
The method
public Object getObjectById(Class clazz, int id)
does only accept a single int column and you statically refer to the "id" field as PK field.
This is a hard restriction on both object model and relational schema.
Maybe it's OK for your current project, but what will you do if the next project has to use a legacy database with compound primary keys.
just my 2 cent,
cheers, Thomas
Youness Afid wrote:
hi, sorry for this noisy mail, but can any guru here tell me if my persistence service is well done ?? thanks a lot in advance
---------------------------------------------------------------------------- ---------------------------- package services.persistence;
import java.util.Collection;
import org.apache.ojb.broker.PersistenceBroker; import org.apache.ojb.broker.PersistenceBrokerFactory; import org.apache.ojb.broker.query.Criteria; import org.apache.ojb.broker.query.Query; import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log;
import services.exception.ExceptionService;
public class PersistenceService implements IPersistenceService {
private Log log = LogFactory.getLog(PersistenceService.class);
private PersistenceBroker getBroker() throws Exception { PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); } catch (Throwable t) { log.error(t); ExceptionService.getInstance().throwException(ExceptionService.FATAL, ExceptionService.OJB_BROKERINSTANCIATION); } return broker; }
public void storeObject(Object object) throws Exception { PersistenceBroker broker = getBroker(); try { broker.beginTransaction(); broker.store(object); broker.commitTransaction(); } catch (Exception e1) { try { broker.abortTransaction(); } catch (Exception e2) { log.error(e2); ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME, ExceptionService.OJB_TRANSACTION); } log.error(e1); ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME, ExceptionService.OJB_STORE); } }
public void deleteObject(Object object) throws Exception { PersistenceBroker broker = getBroker(); try { broker.beginTransaction(); broker.delete(object); broker.commitTransaction(); } catch (Exception e1) { try { broker.abortTransaction(); } catch (Exception e2) { log.error(e2); ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME, ExceptionService.OJB_TRANSACTION); } log.error(e1); ExceptionService.getInstance().throwException(ExceptionService.MESSAGENAME, ExceptionService.OJB_STORE); } }
public Object getObjectByCriteria(Class clazz, Criteria criteria) { Object object = null; Query query = new QueryByCriteria(clazz, criteria); try { object = getBroker().getObjectByQuery(query); } catch (Throwable t) { log.error(t); ExceptionService.getInstance().addError(ExceptionService.MESSAGENAME, ExceptionService.OJB_GETCOLLECTION); } return object; }
public Object getObjectById(Class clazz, int id) { Criteria criteria = new Criteria(); criteria.addEqualTo("id", String.valueOf(id)); return getObjectByCriteria(clazz, criteria); }
public Collection getCollectionByCriteria(Class clazz, Criteria criteria) { Collection collection = null; Query query = new QueryByCriteria(clazz, criteria); try { collection = getBroker().getCollectionByQuery(query); } catch (Throwable t) { log.error(t); ExceptionService.getInstance().addError(ExceptionService.MESSAGENAME, ExceptionService.OJB_GETCOLLECTION); } return collection;
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
