Spring Data JPA 如何在内部工作 [英] How does Spring Data JPA work internally

查看:16
本文介绍了Spring Data JPA 如何在内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Spring Data JPA 教程.我困惑这个框架是如何在内部工作的.让我陈述具体情况

I was going through Spring Data JPA Tutorial. I am confused on how does this framework work internally. Let me state specific scenario

有具体代码

/**
 * Custom finder
 */
public List<Location> getLocationByStateName(String name) {
    @SuppressWarnings("unchecked")
    List<Location> locs = entityManager
        .createQuery("select l from Location l where l.state like :state")
        .setParameter("state", name + "%").getResultList(); // note
        return locs;
}

这只是简单地替换为以下界面

This was simply replaced by following interface

@Repository
public interface LocationJPARepository extends JpaRepository<Location, Long> {
    List<Location> findByStateLike(String stateName);
}

相应的测试用例运行良好

And corresponding test case worked fine

@Test
public void testFindWithLike() throws Exception {
   List<Location> locs = locationRepository.getLocationByStateName("New");
   assertEquals(4, locs.size());
}

新的测试用例

@Test
public void testFindWithLike() throws Exception {
   List<Location> locs = locationJPARepository.findByStateLike("New");
   assertEquals(4, locs.size());
}

我的问题

  • 框架如何知道我是使用 = 查找完全匹配还是使用 SQL 之类的运算符(它不能是方法名称?)进行部分匹配?
  • 如果它以某种方式决定我正在寻找部分匹配,那么仍然有子选项......比如 name% 或 %name 或 %name% ...
  • 还有它如何决定大小写很重要吗?(我可以像 toUpper() 一样使用 SQL 来区分大小写,即通过比较大写的所有内容)
  • (添加问题) 有没有一种方法可以检查日志中的 EXACT SQL 某些地方??
  • How does framework know if i am looking for exact match using = or partial match using SQL like operator (it cant be method name ?)
  • if it somehow decide I am looking for partial match then still there are sub options ... like name% or %name or %name% …
  • Also how it decides case is important in like ? ( i can have case-insensitive by using SQL like with toUpper() i.e. by comparing everything in upper case )
  • (added ques) is there a way i can check the EXACT SQL in log some where ??

希望我能够正确解释我的问题.如果我需要更清楚地说明,请告诉我.

Hope i was able to explain my question properly. Let me know if i need to add in more clarity.

推荐答案

我推荐看一看 查询创建 部分.它非常清楚地解释了规则.

I recommend to take a look at Query Creation section of the reference guide. It explains the rules pretty clearly.

例如,当您想通过名字查找用户并忽略大小写时,您可以使用像 findByFirstnameIgnoreCase 这样的方法名称,它会转换为像 UPPER(x.firstame) = UPPER(?1).

For instance when you want to find User by first name and ignore case, you would use method name like findByFirstnameIgnoreCase which would translate into condition like UPPER(x.firstame) = UPPER(?1).

默认情况下,当你有 findByProperty 方法时,匹配是精确的,所以如果你想有 LIKE 功能,你可以使用方法名称 findByFirstnameLike 这又会转化为条件 where x.firstname like ?1.

By default when you have findByProperty method, the match is exact, so if you want to have LIKE functionality you would use method name findByFirstnameLike which would in turn translate into condition where x.firstname like ?1.

您可以组合这些关键字,但可能会有点疯狂.我个人更喜欢使用 @Query 注释用于更复杂的查询,以避免超长的存储库方法名称.

You can combine these keywords, but it can get a little crazy. Personally I prefer using @Query annotation for more complicated queries to avoid super long repository method names.

这篇关于Spring Data JPA 如何在内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆