Thread: [smartweb-user] Entity -IntegrityReferenceException
Brought to you by:
rlogiacco
From: Pino C. <gco...@gm...> - 2007-06-05 12:46:54
|
Need a control of IntegrityReference when we try to remove an Object from data-base. Maybe the Object is referenced from another table in the database and we must implement a logical remove method. A possible solution for this problem is: 1) Insert IntegrityReferenceException in net.smartlab.web.BusinessObjectFactory in the method remove so the last user is forced to manage this Exception and to implement a logic while removing an Object from DB. 2) Create a method isRemovible(Object o) in the class referenced that can throws an IntegrityReferenceException Es.: I have to remove a CommessaObject from my Db and it's just referenced from a ProgettoObject and AttivitaObject. In ProgettoFactory I've implemented a method "boolean existByCommessa(long id) throws DAOException " and in AttivitaFactory another method "boolean existByCommessa(long id) throws DAOException " then I've used re-call these methods in Domain in the method removeCommessa(String id): public void removeCommessa(String id) throws BusinessException ,IntegrityReferenceException{ =09=09 =09=09CommessaFactory factory =3D CommessaFactory.getInstance(); =09=09try { =09=09=09long idCommessa=3DLong.parseLong(id); =09=09=09if(ProgettoFactory.getInstance().existsByCommessa(idCommessa) || AttivitaFactory.getInstance().existsByCommessa(idCommessa)){ =09=09=09=09logger.error("Errore in isRemovibleCommessa"); =09=09=09=09throw new IntegrityReferenceException("Errore di integrit=C3=A0 referenziale"); =09=09=09} =09=09=09 =09=09=09Commessa toRemove =3D (Commessa) factory.findByKey(Long.parseLong(= id)); =09=09=09factory.remove(toRemove); =09=09} catch (DAOException daoe) { =09=09=09throw new BusinessException(daoe.getMessage()); =09=09} =09} In this way in the CommessaAction ,the method remove may throws an IntegrityReferenceException that we can catch in this way: public String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ActionException { =09=09logger.info("remove di CommessaAction"); =09=09try { =09=09=09String id =3D request.getParameter("id"); =09=09=09Domain.getInstance().removeCommessa(id); =09=09} catch (BusinessException e) { =09=09=09logger.error("errore in remove di CommessaAction", e); =09=09=09throw new ActionException(e.getMessage(), e.getCause()); =09=09} catch (IntegrityReferenceException ire) { =09=09=09logger.error("Non posso eliminare il tipo di commessa- ire"); =09=09=09super.addError(new ActionMessage("errors.progetto.reference"), req= uest); =09=09=09return "failure"; =09=09} =09=09return "success"; =09} It'all..Now It's tima to do it!! --=20 View this message in context: http://www.nabble.com/Entity--IntegrityRefere= nceException-tf3871534s17546.html#a10968695 Sent from the SmartWeb Users mailing list archive at Nabble.com. |
From: rlogiacco <rlo...@us...> - 2007-06-05 13:26:18
|
Pino Contartese wrote: >=20 > Need a control of IntegrityReference when we try to remove an Object from > data-base. > Maybe the Object is referenced from another table in the database and we > must implement a logical remove method. >=20 Well, we actually have such a method into the Hibernate API, the only sad thing is it's quite reverted. When you create the Hibernate Mapping (.hbm file) you can specify the cascading style (more on this on the official Hibernate documentation, specifically on=20 http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#example-pare= ntchild-cascades parent-child cascade and=20 http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-= transitive transitive state ) which can allow you to manage consistency about your objects. Pino Contartese wrote: >=20 > A possible solution for this problem is: > 1) Insert IntegrityReferenceException in > net.smartlab.web.BusinessObjectFactory in the method remove > so the last user is forced to manage this Exception and to implement a > logic while removing an Object from DB. > 2) Create a method isRemovible(Object o) in the class referenced that ca= n > throws an IntegrityReferenceException > Es.: I have to remove a CommessaObject from my Db and it's just reference= d > from a ProgettoObject and AttivitaObject. >=20 What I figure out from your statement is that you wish to handle this cascading manually instead of automatically and beyond your back: am I right? Can I ask you why do you need to handle those things programmatically? For the sake of speeking: do you want to delete all your Progetto and Attivita when you delete Commessa or do you want to avoid deletion of Commessa if there is a Progetto or Attivit=C3=A0 linked to it? In the first case you just need to set the cascade style of your Commessa t= o all-delete-orphan, in the second to something more restictive like all or something lesser. Obviously I coul have not understood your point at all, in which case I jus= k ask you to explain it further... --=20 View this message in context: http://www.nabble.com/Entity--IntegrityRefere= nceException-tf3871534s17546.html#a10969404 Sent from the SmartWeb Users mailing list archive at Nabble.com. |