java - How to check, throw and catch bean's validation ConstraintViolationException -
consider following case:
i have "serivce" module has class called
clientservice.this
clientserviceuses class calledclientdaoin "dao" module.clientdaohas methodinsert(@valid client c). method throwsdaoexception.client entity. attributes annotated javax bean validations,
@javax.validation.constraints.notnull.
if constraint violated, clientservice receives constraintviolationexception. clientservice expects daoexception or other exception of "dao" module. , want keep way, throwing exceptions related directly task object does, hiding implementation details higher layer (in case, "service" module).
what encapsulate javax.validation.constraintviolationexception in validationexception of "dao" module, , declare in trows clause, alongside daoexception. , don't want perform validation checks myself (that's why use @valid annotation)
here code (abstracting interfaces, injections , else. make simple)
package service; class clientservice { insert(client c) throws serviceexception { try { new clientdao().insert(c); } catch( daoexception e) { throw new serviceexception(e); } } } package dao; class clientdao { insert(@valid client c) throws daoexception { myentitymanageroranypersistencestrategy.insert(c); } } i change dao class like:
package dao; class clientdao { insert(@valid client c) throws daoexception, myvalidationexception { myentitymanageroranypersistencestrategy.insert(c); } } but have no idea how in way described.
ftr, i'm using spring web flow , hibernate in project. dao module contains @repository classes , service module contains @service classes.
perhaps don't understand something, guess in case validation performed automatically persistence provider , has nothing @valid annotation.
if so, can catch constraintviolationexception inside dao method:
class clientdao { insert(@valid client c) throws daoexception, myvalidationexception { try { myentitymanageroranypersistencestrategy.insert(c); } catch (constraintviolationexception ex) { throw new myvalidationexception(...); } } }
Comments
Post a Comment