سؤال

I am using Spring Data JPA for my application, which has the following layers:

  1. Service layer with Interfaces and Implements (annotated @service)
  2. CRUD repository layer with Spring Data JPA, together with custom repository implementations
  3. Entity layer

I was wondering where exactly is the correct place to put @Transactional. Currently, I have it in the service layer, where the repositories are being used.

هل كانت مفيدة؟

المحلول

Transactions belong to Service layer. For example if you have HotelService, then the code would look like this:

@Service("hotelService")
@Transactional
public class HotelServiceImpl implements HotelService {
    @Autowired
    HotelDao hotelDao;

    // The rest of code omited ...
}

نصائح أخرى

What is the best with @Transactional you have to put it if you have a database access.

See Understanding the Spring Framework's declarative transaction implementation

you simply to annotate your classes with the @Transactional annotation, add the line (<tx:annotation-driven/>) to your configuration, and then expect you to understand how it all works.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top