I wanted to know when exactly we should use
Код: Выделить всё
@TranasactionalSince JpaRepository's
Код: Выделить всё
save()Код: Выделить всё
@TranasactionalI've seen couple of articles saying that if there is only repository being used inside your service method (like in the below scenario) then it's not required to put
Код: Выделить всё
@TransactionalКод: Выделить всё
save()Код: Выделить всё
@TransactionalКод: Выделить всё
class EmployeeService {
@Autowired
EmployeeRepository repo;
@Transactional //(Is it required here?)
public void saveEmployee(Employee employee) {
repo.save(employee)
}
}
Код: Выделить всё
getStudentsWithNameStartsWithAndGradeLessThan()Код: Выделить всё
@Getter
@Setter
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "average_grade")
private Short averageGrade;
}
public interface StudentRepository extends JpaRepository {
Set findByAverageGradeLessThan(Short averageGrade);
Set findByNameStartsWithIgnoreCase(String name);
}
@Service
public class StudentService {
private final StudentRepository studentRepository;
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@Transactional //(Is it required here?)
public Set getStudentsWithNameStartsWithAndGradeLessThan(String prefix, Short averageGrade) {
Set students = studentRepository.findByNameStartsWithIgnoreCase(prefix);
students.retainAll(studentRepository.findByAverageGradeLessThan(averageGrade));
return students;
}
}
Источник: https://stackoverflow.com/questions/781 ... annotation
Мобильная версия