Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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);
    }
  }

}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.