开发者

Abnormal behaviour by HQL while using JPA (*** More of an information to readers than a question *** )

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!!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜