Когда нам следует использовать аннотацию @Tranasactional?JAVA

Программисты JAVA общаются здесь
Ответить
Гость
 Когда нам следует использовать аннотацию @Tranasactional?

Сообщение Гость »


I wanted to know when exactly we should use

Код: Выделить всё

@Tranasactional
in Spring Boot Services.
Since JpaRepository's method is annotated with

Код: Выделить всё

@Tranasactional
is it really required for me to add that annotation in my service method?
I'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
on service method as is already annotated with

Код: Выделить всё

@Transactional
. Is it true?

Код: Выделить всё

class EmployeeService {

@Autowired
EmployeeRepository repo;

@Transactional //(Is it required here?)
public void saveEmployee(Employee employee) {

repo.save(employee)

}

}
Should it be used in this below case on method

Код: Выделить всё

getStudentsWithNameStartsWithAndGradeLessThan()
since we are using more than one Repository?

Код: Выделить всё

@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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»