简体   繁体   English

Join Fetch在Hibernate查询语言中不起作用

[英]join fetch not working in Hibernate Query Language

I want to retrieve all the records in One database hit and for that, I am using join fetch statements below is my Query 我想检索一个数据库命中中的所有记录,为此,我正在使用join fetch语句,这是我的查询

String q = "SELECT oneChat from " + Chat.class.getName() + " oneChat "
                + " join fetch oneChat.user1 " 
                + " join fetch oneChat.user2 "
                + " join fetch oneChat.user3 "
                + " join fetch oneChat.groupData "
                + "where oneChat.dmlStatus != :dmlStatusValue"
                + " AND group_id = :groupIdValue" + " AND reference_id = 0"
                + " AND root_chat_id = oneChat.chatId";

There are total 4 foreign keys/Joins in my table so I added the join fetch statement but its not working ie not returning anything how ever if I remove the join fetch statements I get the result set. 我的表中总共有4个外键/联接,所以我添加了联接提取语句,但是它不起作用,即如果删除联接提取语句,我什么也不返回任何结果。 My Fetch on table joins is by default Eager ( didn't changed it to Lazy). 默认情况下,我对表联接的访存是Eager(未将其更改为Lazy)。

Also there's no sql syntax error in the Log file. 日志文件中也没有sql语法错误。 Am I missing anything ? 我有什么想念的吗?

Update: It is because the second join ie user2 is returning null so I wasn't getting any data. 更新:这是因为第二个连接,即user2返回null,所以我没有得到任何数据。 Now if anyone could tell me how can I counter this, the query should be independent it shouldn't rely on data. 现在,如果有人可以告诉我如何应对这个问题,那么查询应该是独立的,它不应该依赖数据。

IF you want to return results regardless of data being present on the dependencies, then you should use left join instead of inner join (join fetch is equal to inner join fetch): 如果无论数据是否存在于依赖项上,都希望返回结果,则应使用左联接而不是内部联接(联接获取等于内部联接获取):

"SELECT oneChat from " + Chat.class.getName() + " oneChat "
                + " left join fetch oneChat.user1 " 
                + " left join fetch oneChat.user2 "
                + " left join fetch oneChat.user3 "
                + " left join fetch oneChat.groupData "
                + "where oneChat.dmlStatus != :dmlStatusValue"
                + " AND group_id = :groupIdValue" + " AND reference_id = 0"
                + " AND root_chat_id = oneChat.chatId";

Now when the OneChat does not have any user2 dependency on the database, the query will still return results regardless of that. 现在,当OneChat对数据库没有任何user2依赖关系时,无论如何查询都将返回结果。

Just on the side.. if you are using prefixed, then try to add prefixes to group_id and root_chat_id fields in the where clause for clarity. 只是在一边..如果使用前缀,则为清楚起见,请尝试在where子句的group_idroot_chat_id字段中添加前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM