0

The recommended way of executing SQL queries is by creating a repository, using the @Repository annotation. I'm wondering if I can also execute SQL queries within a service, using the @Service annotation, or is this tied to a specific Spring stereotype?

For example: Is there any rule that guarantees that a @Service class must have business logic and @Repository must have query execution? If I execute a query in a @Service class will it throw any exception?

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
RathanaKumar
  • 325
  • 4
  • 14

5 Answers5

5

No it won't throw any exception. But the idea of separating the DB Logic and Business Logic is to use @Service for service implementations (business logic) and @Repository for repositories i.e to handle DB operations (it can be CRUD, PagingAndSorting etc).

Thus, the code becomes modular and obeys the design patterns and coding standards. Service will make use of Repositories. And your handlers will use methods from your Service. That's how it works.

4

As per the Spring API specification.

A class annotated with @Repository is eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tooling, aspects, etc.

So DataAccessException aims user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC).

@Service does not have any DataAccessException translation thus you can expect un-translated exception on classes which are annotated with @Service annotation. It indicate that a class is a Business Service Facade.

Gaurav Pathak
  • 2,576
  • 1
  • 10
  • 20
2

It will not through any exception there are some specification of each annotation.THIS answer will give you more clarity hope it will help you.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
2

@Service ,@Repository,@Controller are specialization of @Component are all termed as Spring Beans

 @Component  generic stereotype for any Spring-managed component 
 @Repository stereotype for persistence layer                    
 @Service    stereotype for service layer                        
 @Controller stereotype for presentation layer (spring-mvc) 

It's all about distributing the concerns (Presentation,Business,Database),so it won't through any exception as asked by you.

You can refer here for more-Spring Docs

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
1

When we are going to develop any Project it should be lossy coupled and maintainable. To achieve this layer separation is important

@Service - Annotate all your service classes with @Service. This layer knows the unit of work. All your business logic will be in Service classes. Generally, methods of service layer are covered under a transaction. You can make multiple DAO calls from service method. if one transaction fails all transactions should rollback.

@Repository - Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.

@Component - Annotate your other components (for example REST resource classes) with component stereotype.

Reasons to use them :

  • The main advantage of using @Repository or @Service over @Component is that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository.
  • You don't have to write bean definitions in context xml file. Instead annotate classes and use those by autowiring.
  • Specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).

What is Stereotype Refer Here

@Component generic stereotype for any Spring-managed component
@Repository stereotype for persistence layer
@Service stereotype for service layer
@Controller stereotype for presentation layer (spring-mvc)

For More Details Click Here and Here

Romil Patel
  • 12,879
  • 7
  • 47
  • 76