我实际上是在尝试使用 JPA@OneToOne注释将Child实体链接到它的Parent.
它运行良好,除了在获取Childs列表时,JPA 引擎(在本例中为 Hibernate)进行 1+n 次查询。
这是 Hibernate 查询的日志:
select child0_.id as id1_0_, child0_.parent as parent3_0_, child0_.value as value2_0_ from child child0_
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?
Run Code Online (Sandbox Code Playgroud)
使用完全相同的实体定义,特别是当我得到一个孩子时,JPA 使用预期的 JOIN 执行查询:
select child0_.id as id1_0_0_, child0_.parent as parent3_0_0_, child0_.value as …Run Code Online (Sandbox Code Playgroud)