2
\$\begingroup\$

I used Spring MVC arch, and I need to be informed about correct names of methods and exceptions throwing.

/**
 * @author Eugene G. Ustimenko
 * @date Jan 19, 2015
 */
@Service
@Qualifier ("entityService")
public class BaseEntityService < T extends BaseEntity > implements IBaseEntityService < T > {
 @Autowired
 private IBaseEntityDao<T> dao;
 @Transactional
 @Override
 public T create (T entity) throws ServiceException {
 try {
 return dao.create(entity);
 }
 catch (final Exception ex) {
 throw new ServiceException("Create entity operation error " + ex.getMessage(), ex);
 }
 }
 @Transactional
 @Override
 public T update (T entity) throws ServiceException {
 try {
 return dao.update(entity);
 }
 catch (final Exception ex) {
 throw new ServiceException("Update entity operation error " + ex.getMessage(), ex);
 }
 }
 @Transactional
 @Override
 public void remove (Long id) throws ServiceException {
 try {
 dao.remove(id);
 }
 catch (final Exception ex) {
 throw new ServiceException("Remove entity operation error " + ex.getMessage(), ex);
 }
 }
 @Override
 public List < T > getAll () throws ServiceException {
 try {
 return dao.getAll();
 }
 catch (final Exception ex) {
 throw new ServiceException("Get list of entities operation error " + ex.getMessage(), ex);
 }
 }
 @Override
 public T get (Long id) throws ServiceException {
 try {
 return dao.get(id);
 }
 catch (final Exception ex) {
 throw new ServiceException("Get entity by identifier operation error" + ex.getMessage(), ex);
 }
 }
}
Simon Forsberg
59.7k9 gold badges157 silver badges311 bronze badges
asked Aug 23, 2015 at 17:39
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you really, really want to catch an unchecked exception that might be thrown when using a database, you can catch DataAccessException which is the higher exception on Spring's data translation exception hierarchy.

For correct method names, there no such thing on Spring, just use a name which can clearly describe what your method does, your current names are pretty good too. One more note, use the Service layer to aggregate data or do complicated business logic, and place simple logics on the domain, to avoid the Anemic Domain Model

You can find more info here about the last statement around Spring

answered Aug 23, 2015 at 19:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.