我有org.springframework.boot:spring-boot-starter-parent:2.3.4.RELEASE
作为我的父pom和spring-boot-starter-data-jpa
作为我的依赖项。
在City city2 = cityRepository.findById(20L).orElseGet(null);
行上有一个断点
首先,我调用方法m2
,程序挂起断点,它显示city1.name
是ocean
。
然后,我用参数name = land
调用方法m1
,它存储在后端数据库中。
最后,我恢复断点,但city2.name
仍然是ocean
!如果我通过在application.properties
中添加spring.jpa.show-sql=true
来启用jpa日志,则表明程序永远不会在City city2 = cityRepository.findById(20L).orElseGet(null);
行上查询后端数据库
我很困惑,因为city2
是陈旧的!
下面是我的源代码:Controller.java
个
@PostMapping("/m1")
@Transactional(propagation = Propagation.NEVER)
public Object m1(@RequestParam String name){
City city = new City();
city.setId(20L);
city.setName(name);
city.setState("on");
cityRepository.save(city);
return city;
}
@GetMapping("/m2")
@Transactional(propagation = Propagation.NEVER)
public Object m2(){
City city1 = cityRepository.findById(20L).orElseGet(null);
// breakpoint here
City city2 = cityRepository.findById(20L).orElseGet(null);
return city2;
}
字符串City.java
个
@Data
@Entity
@Cacheable(false)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONE)
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
}
型
1条答案
按热度按时间pu82cl6c1#
您是否使用相同的输入参数调用存储库,以便返回相同的结果