HQL 查询在 Spring 的 @Query 注解中

huangapple go评论73阅读模式
英文:

HQL Query inside spring @Query annotation

问题

我一直在努力理解HQL和JPQL之间的区别。这里的Hibernate文档建议,在JPQL中写入select是必需的,但在HQL中不需要。但是,当我尝试在Spring Data JPA的Query注解中编写HQL或JPQL时,HQL和JPQL都可以工作。

import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {
    
    @Query(value = "from com.demo.spring.data.jpa.User u")
    public List<User> findAllUsersHQL();
    
    @Query(value = "select u from com.demo.spring.data.jpa.User u")
    public List<User> findAllUsersJPQL();
}

我的理解是,由于Query注解旨在用于JPA,为什么会支持HQL?

或者换句话说 -

我们可以在Query注解中编写HQL吗 - Spring Data JPA是否支持两者?

英文:

I have been trying to understand the difference between HQL and JPQL. The hibernate documentation at here

suggest that writing select is necessary for JPQL but not in HQL. But when I try writing HQL or JPQL inside Query annotation of spring data JPA , both HQL and JPQL works .

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository&lt;User, Long&gt; {
	
	@Query(value = &quot;from com.demo.spring.data.jpa.User u&quot;)
	public List&lt;User&gt; findAllUsersHQL();
	
	@Query(value = &quot;select u from com.demo.spring.data.jpa.User u&quot;)
	public List&lt;User&gt; findAllUsersJPQL();
}

My understanding is since the Query annotation is intended for JPA why is HQL supported ?

Or let me put in other words -

Can we write HQL in the Query annotation - Does spring data JPA supports both ?

答案1

得分: 2

我认为这取决于你使用的JPA实现。

尽管Spring Data表示支持JPQL,但Spring Data本身并没有实现任何用于验证和执行JPQL查询的功能。

Spring将依赖底层的JPA实现来验证并将给定的JPQL查询转换为普通的SQL。

如果底层适配器是hibernate Adapter,那么它很有可能支持HQL,并且不会限制应用程序只使用JPQL。

此外,正如jdicker在上面提到的,JPQL是HQL的子集,换句话说,Hibernate提供的功能比JPA要多得多,因此只要你使用Hibernate作为底层的JPA引擎,你应该能够在Spring Data中解析HQL。

自己动手 - 你可以尝试在QueryTranslatorImpl中设置断点,自行确认其行为。

英文:

I think it depends on which JPA impleemntation you use.

Even though Spring data says it supports JPQL, Spring data in itself isnt implememnting any functionality for validating and implementing queries with JPQL.

Spring will rely on underlying JPA impleemntation that will validate and convert given JPQL queries into plain SQL.

If the underlying adapter is a hibernate Adapter, There are fair chances that it will support HQL and will not limit the application to JPQL.

Moreover, as jdicker mentioned above, JPQL is a subset of HQL, or in the other words, hibernate provided mucm more functionalty than that of JPA.. so you shoudl be able to parse HQL in spring data as long as you are using Hibernate sa an underlying JPA engine.

DIY - You can try putting a breakpoint in QueryTranslatorImpl and confirm the behavior yourself.

huangapple
  • 本文由 发表于 2020年7月30日 12:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63165994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定