I have simple code example with Spring Data:
Entity
@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue
private Long id;
private String name;
//...
}
Repository:
@Repository
public interface CarRepository extends JpaRepository<Car, Long> {
}
Service with example methods:
@Service
public class CarService {
@Autowired
private CarRepository carRepository;
public void change() {
Car one = carRepository.findOne(1L);
one.setName("changed"); //important
}
}
And controller:
@RestController("/")
public class CarController {
@Autowired
private CarRepository carRepository;
@Autowired
private CarService carService;
@GetMapping
public List<Car> home() {
carService.change();
List<Car> all = carRepository.findAll();
return all;
}
}
I also have data.sql with 3 records:
insert into car values (1, 'aaa');
insert into car values (2, 'bbb');
insert into car values (3, 'ccc');
When I send request to the localhost:8080 I get list with three cars, where the first one is updated:
insert into car values (1, 'changed');
insert into car values (2, 'bbb');
insert into car values (3, 'ccc');
My expectation is that after calling change() from controller actually nothing happens because this method is not transactional. So why my home() method in controller returns list with updated entity?
This question is completly different from this Spring data jpa @transactional I don't understand why you mark this as duplicate.