前言
使用@EntityGraph 关联多个数据
@EntityGraph(attributePaths = {
"userContact"
,"userDetail"
,"authorities"
,"authorities.resources"
,"userAccounts"
,"dispositions"
,"favoriteFoodGenres"
,"favoriteFilmGenres"
,"favoriteMusicGenres"
,"favoriteGameGenres"
,"languageLevel"
,"userSocialInfo"
,"userStatistic"
,"userPassword"
})
Optional<User> findById(Long id);
多个多对多导致 10来条数据,查出了7200 条数据。
下面是hibernate 生成的sql语句:
select user0_.id as id1_81_0_, user0_.avatar as avatar2_81_0_, user0_.background_image as backgrou3_81_0_, user0_.birthday as birthday4_81_0_, user0_.create_time as create_t5_81_0_, user0_.language_level_id as languag11_81_0_, user0_.level as level6_81_0_, user0_.name as name7_81_0_, user0_.sex as sex8_81_0_, user0_.signature as signatur9_81_0_, user0_.update_time as update_10_81_0_, user0_.user_contact_id as user_co12_81_0_, user0_.user_detail_id as user_de13_81_0_, user0_.user_password_id as user_pa14_81_0_, user0_.user_social_info_id as user_so15_81_0_, user0_.user_statistic_id as user_st16_81_0_, authoritie1_.user_id as user_id1_83_1_, authority2_.id as authorit2_83_1_, authority2_.id as id1_3_2_, authority2_.create_time as create_t2_3_2_, authority2_.description as descript3_3_2_, authority2_.enable as enable4_3_2_, authority2_.name as name5_3_2_, authority2_.update_time as update_t6_3_2_, authority2_.weight as weight7_3_2_, resources3_.authority_id as authorit1_4_3_, resource4_.id as resource2_4_3_, resource4_.id as id1_58_4_, resource4_.create_time as create_t2_58_4_, resource4_.description as descript3_58_4_, resource4_.method as method4_58_4_, resource4_.name as name5_58_4_, resource4_.url as url6_58_4_, dispositio5_.user_id as user_id1_87_5_, dispositio6_.id as disposit2_87_5_, dispositio6_.id as id1_23_6_, dispositio6_.create_time as create_t2_23_6_, dispositio6_.enable as enable3_23_6_, dispositio6_.`index` as index4_23_6_, dispositio6_.name as name5_23_6_, dispositio6_.name_en as name_en6_23_6_, dispositio6_.update_time as update_t7_23_6_, dispositio6_.user_id as user_id8_23_6_, favoritefi7_.user_id as user_id1_88_7_, favoritefi8_.id as favorite2_88_7_, favoritefi8_.id as id1_25_8_, favoritefi8_.create_time as create_t2_25_8_, favoritefi8_.enable as enable3_25_8_, favoritefi8_.`index` as index4_25_8_, favoritefi8_.name as name5_25_8_, favoritefi8_.name_en as name_en6_25_8_, favoritefi8_.update_time as update_t7_25_8_, favoritefi8_.user_id as user_id8_25_8_, favoritefo9_.user_id as user_id1_89_9_, favoritefo10_.id as favorite2_89_9_, favoritefo10_.id as id1_26_10_, favoritefo10_.create_time as create_t2_26_10_, favoritefo10_.enable as enable3_26_10_, favoritefo10_.`index` as index4_26_10_, favoritefo10_.name as name5_26_10_, favoritefo10_.name_en as name_en6_26_10_, favoritefo10_.update_time as update_t7_26_10_, favoritefo10_.user_id as user_id8_26_10_, favoritega11_.user_id as user_id1_90_11_, favoritega12_.id as favorite2_90_11_, favoritega12_.id as id1_27_12_, favoritega12_.create_time as create_t2_27_12_, favoritega12_.enable as enable3_27_12_, favoritega12_.`index` as index4_27_12_, favoritega12_.name as name5_27_12_, favoritega12_.name_en as name_en6_27_12_, favoritega12_.update_time as update_t7_27_12_, favoritega12_.user_id as user_id8_27_12_, favoritemu13_.user_id as user_id1_91_13_, favoritemu14_.id as favorite2_91_13_, favoritemu14_.id as id1_28_14_, favoritemu14_.create_time as create_t2_28_14_, favoritemu14_.enable as enable3_28_14_, favoritemu14_.`index` as index4_28_14_, favoritemu14_.name as name5_28_14_, favoritemu14_.name_en as name_en6_28_14_, favoritemu14_.update_time as update_t7_28_14_, favoritemu14_.user_id as user_id8_28_14_, languagele15_.id as id1_42_15_, languagele15_.create_time as create_t2_42_15_, languagele15_.enable as enable3_42_15_, languagele15_.`index` as index4_42_15_, languagele15_.level as level5_42_15_, languagele15_.name as name6_42_15_, languagele15_.name_en as name_en7_42_15_, languagele15_.update_time as update_t8_42_15_, languagele15_.user_id as user_id9_42_15_, useraccoun16_.user_id as user_id6_82_16_, useraccoun16_.id as id1_82_16_, useraccoun16_.id as id1_82_17_, useraccoun16_.create_time as create_t2_82_17_, useraccoun16_.enable as enable3_82_17_, useraccoun16_.subject as subject4_82_17_, useraccoun16_.subject_id as subject_5_82_17_, useraccoun16_.user_id as user_id6_82_17_, usercontac17_.id as id1_85_18_, usercontac17_.area as area2_85_18_, usercontac17_.city as city3_85_18_, usercontac17_.country as country4_85_18_, usercontac17_.create_time as create_t5_85_18_, usercontac17_.detailed as detailed6_85_18_, usercontac17_.emails as emails7_85_18_, usercontac17_.image as image8_85_18_, usercontac17_.phones as phones9_85_18_, usercontac17_.province as provinc10_85_18_, usercontac17_.street as street11_85_18_, usercontac17_.third as third12_85_18_, usercontac17_.update_time as update_13_85_18_, usercontac17_.user_id as user_id14_85_18_, userdetail18_.id as id1_86_19_, userdetail18_.account_non_expired as account_2_86_19_, userdetail18_.account_non_locked as account_3_86_19_, userdetail18_.credentials_non_expired as credenti4_86_19_, userdetail18_.logoff as logoff5_86_19_, userdetail18_.non_certificated as non_cert6_86_19_, userdetail18_.non_mobile_certificated as non_mobi7_86_19_, userdetail18_.user_id as user_id8_86_19_, userpasswo19_.id as id1_94_20_, userpasswo19_.create_time as create_t2_94_20_, userpasswo19_.non_expired as non_expi3_94_20_, userpasswo19_.password as password4_94_20_, userpasswo19_.salt as salt5_94_20_, userpasswo19_.update_time as update_t6_94_20_, userpasswo19_.user_id as user_id7_94_20_, usersocial20_.id as id1_95_21_, usersocial20_.constellation as constell2_95_21_, usersocial20_.create_time as create_t3_95_21_, usersocial20_.department as departme4_95_21_, usersocial20_.department_code as departme5_95_21_, usersocial20_.enrollment_time as enrollme6_95_21_, usersocial20_.profession as professi7_95_21_, usersocial20_.profession_code as professi8_95_21_, usersocial20_.school as school9_95_21_, usersocial20_.school_code as school_10_95_21_, usersocial20_.school_type as school_11_95_21_, usersocial20_.trade as trade12_95_21_, usersocial20_.trade_code as trade_c13_95_21_, usersocial20_.user_id as user_id14_95_21_, userstatis21_.id as id1_104_22_, userstatis21_.create_time as create_t2_104_22_, userstatis21_.fan_count as fan_coun3_104_22_, userstatis21_.favorite_count as favorite4_104_22_, userstatis21_.follow_count as follow_c5_104_22_, userstatis21_.friend_count as friend_c6_104_22_, userstatis21_.like_count as like_cou7_104_22_, userstatis21_.moment_count as moment_c8_104_22_, userstatis21_.update_time as update_t9_104_22_, userstatis21_.user_level as user_le10_104_22_, userstatis21_.video_count as video_c11_104_22_ from user user0_ left outer join user_authority authoritie1_ on user0_.id=authoritie1_.user_id left outer join authority authority2_ on authoritie1_.authority_id=authority2_.id left outer join authority_resource resources3_ on authority2_.id=resources3_.authority_id left outer join resource resource4_ on resources3_.resource_id=resource4_.id left outer join user_disposition dispositio5_ on user0_.id=dispositio5_.user_id left outer join disposition dispositio6_ on dispositio5_.disposition_id=dispositio6_.id left outer join user_favorite_film_genre favoritefi7_ on user0_.id=favoritefi7_.user_id left outer join favorite_film_genre favoritefi8_ on favoritefi7_.favorite_film_genre_id=favoritefi8_.id left outer join user_favorite_food_genre favoritefo9_ on user0_.id=favoritefo9_.user_id left outer join favorite_food_genre favoritefo10_ on favoritefo9_.favorite_food_genre_id=favoritefo10_.id left outer join user_favorite_game_genre favoritega11_ on user0_.id=favoritega11_.user_id left outer join favorite_game_genre favoritega12_ on favoritega11_.favorite_game_genre_id=favoritega12_.id left outer join user_favorite_music_genre favoritemu13_ on user0_.id=favoritemu13_.user_id left outer join favorite_music_genre favoritemu14_ on favoritemu13_.favorite_music_genre_id=favoritemu14_.id left outer join language_level languagele15_ on user0_.language_level_id=languagele15_.id left outer join user_account useraccoun16_ on user0_.id=useraccoun16_.user_id left outer join user_contact usercontac17_ on user0_.user_contact_id=usercontac17_.id left outer join user_detail userdetail18_ on user0_.user_detail_id=userdetail18_.id left outer join user_password userpasswo19_ on user0_.user_password_id=userpasswo19_.id left outer join user_social_info usersocial20_ on user0_.user_social_info_id=usersocial20_.id left outer join user_statistic userstatis21_ on user0_.user_statistic_id=userstatis21_.id where user0_.id=43
image.png
总结
1.不在使用@EntityGraph 直接显示关联多个manyToMany 或 OneToMany
2.像我现在业务场景完成可以修改为数组,就算呀兼容标签多语音,最多就加两个数组。
网友评论