英文:
Testing Fetching Strategies with Hibernate annotations
问题
我想在我的Spring Boot项目中测试两个相关类上的不同注释。IDE是Intellij,构建工具是gradle。
我了解,默认情况下,@ManyToOne
和@OneToOne
是急加载的。我想看看当我覆盖它时会有什么区别。
在浏览器中使用控制器测试时,查看结果需要耗费时间,而Jackson会序列化响应。
实体类
机械师访问
@Entity
@Data
@Table(name="MechanicVisit")
public class MechanicVisit {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@JsonManagedReference
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Car car;
汽车
@Entity
@Data
public class Car {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@JsonBackReference
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "car")
private List<MechanicVisit> listMechanicVisit = new ArrayList<>();
//others
以及我手动在存储库中获取MechanicVisit的方法(不应该获取汽车):
@Query("SELECT m FROM MechanicVisit m WHERE m.id = :id")
Optional<MechanicVisit> getOneManually(@Param("id") Long id);
如何在Spring生成的测试结构(位于/src/main/java)中快速测试不同的获取策略,如fetch = FetchType.LAZY
,而不必一直启动和停止服务器?
我注意到它已生成了一个测试类:
@SpringBootTest
class CarApplicationTests {
@Test
void contextLoads() {
}
}
英文:
I would like to test different annotations on two related classes in my Spring boot project. The IDE is Intellij with gradle as the build tool.
I understand that by default @ManyToOne and @OneToOne
are fetched eagerly. I want to see the difference this makes when I override it.
When testing this in the browser using the controller it is both time consuming to see results and Jackson serialises the responses.
ENTITIES
Mechanic Visit
@Entity
@Data
@Table(name="MechanicVisit")
public class MechanicVisit {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@JsonManagedReference
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Car car;
Car
@Entity
@Data
public class Car {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@JsonBackReference
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "car")
private List<MechanicVisit> listMechanicVisit = new ArrayList<>();
//others
And my manual method to get a MechanicVisit on the repository (which should not get a car):
@Query("SELECT m FROM MechanicVisit m WHERE m.id = :id")
Optional <MechanicVisit> getOneManually(@Param("id") Long id);
How can I quickly test the effects of different fetching strategies like fetch = FetchType.LAZY
in the Spring generated test structure at /src/main/java without starting and stopping the server all the time?
I note it has generated a test class:
@SpringBootTest
class CarApplicationTests {
@Test
void contextLoads() {
}
}
答案1
得分: 1
答案是手动实例化实体管理器,以调试模式设置断点,然后评估各种查询:
仓库
public class MechanicVisitServiceImpl implements MechanicVisitService {
private final MechanicVisitRepository mechanicVisitRepository;
@Autowired
private EntityManager entityManager;
//其他查询
}
然后在Intellij中,当您的代码遇到断点时,您可以使用CTRL-F8
来评估表达式,如:
entityManager.createQuery("SELECT m FROM MechanicVisit m WHERE m.id=1").getResultList()
英文:
The answer was to instantiate Entity Manager manually, set a breakpoint in debug mode and then evaluate various queries:
Repository
public class MechanicVisitServiceImpl implements MechanicVisitService {
private final MechanicVisitRepository mechanicVisitRepository;
@Autowired
private EntityManager entityManager;
//other queries
Then in Intellij when your code hits a breakpoint you can CTRL-F8
to evaulate expressions like:
entityManager.createQuery("SELECT m FROM MechanicVisit m WHERE m.id=1").getResultList()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论