Let's 을 고려한 봄 부팅(Spring Data JPA)응용 프로그램으로 컨트롤러 및 서비스에서 아래 예제:
processEntity()
를 가져옵체에서 데이터베이스 및 프로세스/업데이트 할 것입니다.publishEntity()
별도의 방법을 다시 가져오는 객체 데이터베이스에서,그리고 출판이 그것을 다른 시스템입니다. 그것을 필요로"최신"상태의 엔티티 데이터베이스에서.
나는 여러 가지를 발견했 블로그 게시물과 기사에는 권장 하지 않으로 운영하는 엔티티에서 컨트롤러,즉 범위 밖에서 트랜잭션이 있습니다. 그래서 수업과 같습니다:
@Service
public SomeService {
@Transactional
public void processEntity(long id) {
final SomeEntity someEntity = entityRepository.findById(id);
// process entity...
entityRepository.save(someEntity);
}
@Transactional(readOnly = true)
public void publishEntity(long id) {
final SomeEntity someEntity = entityRepository.findById(id);
// publish entity to another system...
}
}
@RestController
public SomeController {
@GetMapping(path = "/api/entity")
public ResponseEntity<Void> getEntity() {
someService.processEntity(1);
someService.publishEntity(1);
}
}
은 이 방법은 합리적입니까? 또는 있는 더 나은 패턴이 이러한 사용할 경우? 그것은 것"으로 간주 나쁜 연습으면" processEntity
반 entity 자체 컨트롤러는 전달하기 publishEntity
?