使用 JPA 时 HQL 的异常行为(*** 向读者提供的信息多于问题 *** )

发布于 2024-09-12 09:38:35 字数 689 浏览 2 评论 0原文

我试图在基于 JPA 的应用程序中出于某些报告目的而触发 hql 。 我尝试的以下查询在启动时不断出现问题(因为我将其作为 NamedQuery 给出,所以在启动时检查了语法)。

不正确的查询:

SELECT t FROM Table_1 tb1 
INNER JOIN 
Table_2 tb2
where tb1.name = 'someName';

经过大量的排列和组合(并深入研究了查询的其他部分,这是我之前怀疑的),我意识到问题出在“t” 。 正确的查询应该是而不是上面的查询...

SELECT tb1 FROM Table_1 tb1
INNER JOIN 
Table_2 tb2
where tb1.name = 'someName';

请注意,HQL 期望 tb1 与表短名“tb1”相同>”

tableName 缩写和选择不匹配只会导致我面临的问题。令人惊讶的是,hibernate/JPA 并没有在堆栈中给出错误,但部署继续运行,我最终需要终止 java 进程才能退出它。

希望这对某人有帮助。

如果有人也能解释这种行为,那就太好了。

谢谢!!

I was trying to fire hql for some reporting purpose in my JPA based application.
The following query that I tried kept conking out on startup (Since I had given it as a NamedQuery, it was checked for syntax at startup).

Incorrect Query:

SELECT t FROM Table_1 tb1 
INNER JOIN 
Table_2 tb2
where tb1.name = 'someName';

After lot of permutations and combinations (and digging through the other parts of the query, which I suspected earlier), I realized the problem lied in the "t". Instead of the query above, the correct query should have been...

SELECT tb1 FROM Table_1 tb1
INNER JOIN 
Table_2 tb2
where tb1.name = 'someName';

Notice that HQL expects the tb1 to be same as the Table shortname "tb1"

The tableName shortform and the select mismatch only resulted in the issue that I was facing. Surprisingly, hibernate/JPA does not give an error in the stack, but the deployment keeps on running through and I need to ultimately kill the java process to get out of it.

Hope this helps someone.

It would great, if somebody could also reason this behaviour.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-09-19 09:38:36

处理此类语句的最佳方法是将“变量”放在 SELECT 子句中与表别名相同的实践中。
那不是

"SELECT t from Table_1 T1" 

发表像

"SELECT T1 from Table_1 T1"

“希望”这样对未来有帮助的声明。

The best way to deal with such statements it make it a PRACTICE to put the "Variable" in the SELECT clause same as the table Alias.
That is instead of

"SELECT t from Table_1 T1" 

make a statment like

"SELECT T1 from Table_1 T1"

Hope that helps someone in the future.

緦唸λ蓇 2024-09-19 09:38:35

我不确定是否理解这个问题,但如果您认为 Hibernate 应该以在 EntityManagerFactory 创建时报告(或不报告)语法错误的 NamedQuery 的方式进行改进,你应该创建一个问题。但我不认为它会得到高度重视。

I'm not sure to understand the question but if you think that Hibernate should be improved in the way it reported (or not) your syntactically incorrect NamedQuery at EntityManagerFactory creation time, you should create an issue. I don't think it will get a high priority though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文