Java 一对一的关系不起作用
Hibernate版本-5.3.4.Final,mysql连接器版本-8.0.12 我在帖子和帖子内容之间有一个@OneToOne关系: 职位: 这个maven插件:Java 一对一的关系不起作用,java,hibernate,jakarta-ee,orm,Java,Hibernate,Jakarta Ee,Orm,Hibernate版本-5.3.4.Final,mysql连接器版本-8.0.12 我在帖子和帖子内容之间有一个@OneToOne关系: 职位: 这个maven插件: <plugin> <groupId>org.hibernate.orm.tooling</groupId> <artifactId>hibernate-enhance-maven-plugin</artifactI
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>5.3.4.Final</version>
<executions>
<execution>
<configuration>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
Hibernate sql:
Hibernate: select postsinfo0_.post_id as post_id1_9_0_, postsinfo0_.createDate as createDa2_9_0_, postsinfo0_.title as title3_9_0_, postsinfo0_.user_id as user_id5_9_0_, postsinfo0_.views as views4_9_0_, postimage1_.post_id as post_id1_6_1_, postimage1_.image_l as image_l2_6_1_, postimage1_.image_m as image_m3_6_1_, postimage1_.image_s as image_s4_6_1_ from postsInfo postsinfo0_ left outer join postImage postimage1_ on postsinfo0_.post_id=postimage1_.post_id where postsinfo0_.post_id=?
Hibernate: select postconten0_.post_id as post_id1_5_0_, postconten0_.content as content2_5_0_, postconten0_.subtitle as subtitle3_5_0_ from postContent postconten0_ where postconten0_.post_id=?
Hibernate: select postinside0_.post_id as post_id3_7_0_, postinside0_.image_id as image_id1_7_0_, postinside0_.image_id as image_id1_7_1_, postinside0_.image as image2_7_1_, postinside0_.post_id as post_id3_7_1_ from postInsideImages postinside0_ where postinside0_.post_id=?
Hibernate: select tagspost0_.post_id as post_id1_10_0_, tagspost0_.tag_id as tag_id2_10_0_, tags1_.tag_id as tag_id1_15_1_, tags1_.description as descript2_15_1_, tags1_.name as name3_15_1_ from PostsTags tagspost0_ inner join Tags tags1_ on tagspost0_.tag_id=tags1_.tag_id where tagspost0_.post_id=?
我能做些什么来获得延迟加载?@Danil Eltsov,这意味着您要对相关集合执行另一个查询。延迟加载无论如何都会生成另一个查询 您的数据库中有设计的味道。因为您没有与父表PK共享子表PK。一对一关联始终共享PK。在您的情况下,PostContent类的postId应仅由PostsInfo类的postId映射。但在PostContent中,PostsInfo有单独的PK和FK,这对于一对一的关系应该是相同的,hibernate也提倡这样做。因为如果你有单独的FK和PK,那么一个PostsInfo可能有多个PostContent。 请浏览以下链接,因为其中解释的场景与您的场景非常相似
我会避免完全使用一对一-它们往往会使数据结构复杂化,有时会造成性能问题。如果您正在使用spring的DTO/DAO系统,您可以稍后将数据分为不同的类。@Kilves,现在我使用的是java ee堆栈,如果您建议如何最好地组织数据,那么我很乐意提供任何帮助。我接受了@OneTONE决不会懒散地获取数据,无论您做什么。最后我使用了投影。@user2215545,如果您的意思是从post p中选择new project.dto.post.id,p.name,那么我有一个如何加载集合的问题。如果我需要一个此post的集合n,那么我必须为每个post发出n个请求?这意味着您要为每个post发出一个请求。正如您所见,Hibernate为您生成了4个查询,其中一个查询有一个连接,我认为这是您试图避免的。与代表集合的OneToMany相关的实体在默认情况下将发出一个急切加载。然后,Hibernate会在只有您需要时发出查询。通过投影,您可以手动完成,因此您可以优化查询并避免休眠生成您可能不需要的查询。我正确理解,要接收帖子内容,必须发出第二个请求?然后我可以使用继承加入策略,但我需要一个请求
<property name="hibernate.enhancer.enableLazyInitialization">true</property>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>5.3.4.Final</version>
<executions>
<execution>
<configuration>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
@Override
public PostsInfo getCommonPost(long id){
return session.find(PostsInfo.class, id);
}
Hibernate: select postsinfo0_.post_id as post_id1_9_0_, postsinfo0_.createDate as createDa2_9_0_, postsinfo0_.title as title3_9_0_, postsinfo0_.user_id as user_id5_9_0_, postsinfo0_.views as views4_9_0_, postimage1_.post_id as post_id1_6_1_, postimage1_.image_l as image_l2_6_1_, postimage1_.image_m as image_m3_6_1_, postimage1_.image_s as image_s4_6_1_ from postsInfo postsinfo0_ left outer join postImage postimage1_ on postsinfo0_.post_id=postimage1_.post_id where postsinfo0_.post_id=?
Hibernate: select postconten0_.post_id as post_id1_5_0_, postconten0_.content as content2_5_0_, postconten0_.subtitle as subtitle3_5_0_ from postContent postconten0_ where postconten0_.post_id=?
Hibernate: select postinside0_.post_id as post_id3_7_0_, postinside0_.image_id as image_id1_7_0_, postinside0_.image_id as image_id1_7_1_, postinside0_.image as image2_7_1_, postinside0_.post_id as post_id3_7_1_ from postInsideImages postinside0_ where postinside0_.post_id=?
Hibernate: select tagspost0_.post_id as post_id1_10_0_, tagspost0_.tag_id as tag_id2_10_0_, tags1_.tag_id as tag_id1_15_1_, tags1_.description as descript2_15_1_, tags1_.name as name3_15_1_ from PostsTags tagspost0_ inner join Tags tags1_ on tagspost0_.tag_id=tags1_.tag_id where tagspost0_.post_id=?