1
\$\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);
    }
  }

}
\$\endgroup\$
1
\$\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

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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