i have rest web application.
when performing put request on resource called thinggroupmy controller calls service layer that
@requestmapping(method = requestmethod.put) @responsebody public void updatethinggroup(@valid @requestbody final thinggroup thinggroup, final principal principal) throws unknownresourceexception { final user user = userservice.findbyusername(principal.getname()); thinggroupservice.update(thinggroup, user); } and corresponding thinggroupservice @transactional
@override @transactional public thinggroup update(final thinggroup thinggroup, user user){ thinggroup found = null; try { found = find(thinggroup.getid(),user); found.setname(thinggroup.getname()); found = getdao().save(found); } catch (final dataintegrityviolationexception e) { // want throw custom exception here mybadrequestexception ex = new mybadrequestexception("mybadrequestexception.update.violation.thinggroup", "violation_data_integrity"); ex.setparameters(new object[] { thinggroup.getname()}); throw ex; } return found; } in mysql underlying database have constraint of unicity on name of thinggroup.
so if try update thinggroup existing name, should fail.
when fails, i'd throw custom exception pass understandable message controller layer.
my problem that, because of @transactional annotation, hibernate exceptions thrown after transaction has been committed, means after service method has returned (i think).
and means can't catch exception in service layer.
i have search answers , have understood, need catch exception in controller layer. doesn't seem solution, because controller layer have aware of underlying behavior of service layer, not good.
is there general solution issue ?
you can manually flush modifications database force constraint violation occur before automatic flush happens @ end of transaction.
Comments
Post a Comment