like 子句 JPQL 中的参数

发布于 2024-08-03 01:18:22 字数 318 浏览 5 评论 0原文

我正在尝试使用 like 子句编写 JPQL 查询:

LIKE '%:code%'

我想要 code=4 并发现

455
554
646
...

我无法传递 :code = '%value%'

namedQuery.setParameter("%" + this.value + "%");

因为在另一个地方我需要 :value 未被 % 字符包裹。有什么帮助吗?

I am trying to write a JPQL query with a like clause:

LIKE '%:code%'

I would like to have code=4 and find

455
554
646
...

I cannot pass :code = '%value%'

namedQuery.setParameter("%" + this.value + "%");

because in another place I need :value not wrapped by the % chars. Any help?

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

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

发布评论

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

评论(10

身边 2024-08-10 01:18:22

如果您这样做

LIKE :code

然后再这样做

namedQuery.setParameter("code", "%" + this.value + "%");

,则值将不受“%”符号的影响。如果您需要在同一查询中的其他地方使用它,只需使用除 'code' 之外的另一个参数名称。

If you do

LIKE :code

and then do

namedQuery.setParameter("code", "%" + this.value + "%");

Then value remains free from the '%' sign. If you need to use it somewhere else in the same query simply use another parameter name other than 'code' .

九局 2024-08-10 01:18:22

我不会对所有查询使用命名参数。例如,在 JpaRepository

为了解决这个问题,我使用 JPQL CONCAT 函数(此代码模拟 start with):

@Repository
public interface BranchRepository extends JpaRepository<Branch, String> {
    private static final String QUERY = "select b from Branch b"
       + " left join b.filial f"
       + " where f.id = ?1 and b.id like CONCAT(?2, '%')";
    @Query(QUERY)
    List<Branch> findByFilialAndBranchLike(String filialId, String branchCode);
}

我在优秀的文档中找到了这项技术:http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs /manual/jpa_overview_query.html

I don't use named parameters for all queries. For example it is unusual to use named parameters in JpaRepository.

To workaround I use JPQL CONCAT function (this code emulate start with):

@Repository
public interface BranchRepository extends JpaRepository<Branch, String> {
    private static final String QUERY = "select b from Branch b"
       + " left join b.filial f"
       + " where f.id = ?1 and b.id like CONCAT(?2, '%')";
    @Query(QUERY)
    List<Branch> findByFilialAndBranchLike(String filialId, String branchCode);
}

I found this technique in excellent docs: http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/jpa_overview_query.html

娇纵 2024-08-10 01:18:22

您可以使用 JPA LOCATE 函数

LOCATE(searchString, CandidateString [, startIndex]):返回
候选字符串中搜索字符串的第一个索引。位置是从 1 开始的。
如果未找到该字符串,则返回 0。

仅供参考: 上的文档我的热门 Google 搜索 的参数颠倒了。

SELECT 
  e
FROM 
  entity e
WHERE
  (0 < LOCATE(:searchStr, e.property))

You could use the JPA LOCATE function.

LOCATE(searchString, candidateString [, startIndex]): Returns the
first index of searchString in candidateString. Positions are 1-based.
If the string is not found, returns 0.

FYI: The documentation on my top google hit had the parameters reversed.

SELECT 
  e
FROM 
  entity e
WHERE
  (0 < LOCATE(:searchStr, e.property))
荆棘i 2024-08-10 01:18:22

JPA 标准 API 中有一个很好的 like() 方法。尝试使用它,希望它会有所帮助。

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = cb.createQuery(Employees.class);
Root<Employees> rootOfQuery = criteriaQuery.from(Employees.class);
criteriaQuery.select(rootOfQuery).where(cb.like(rootOfQuery.get("firstName"), "H%"));

There is nice like() method in JPA criteria API. Try to use that, hope it will help.

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = cb.createQuery(Employees.class);
Root<Employees> rootOfQuery = criteriaQuery.from(Employees.class);
criteriaQuery.select(rootOfQuery).where(cb.like(rootOfQuery.get("firstName"), "H%"));
巾帼英雄 2024-08-10 01:18:22

我不知道我是否迟到或超出范围,但在我看来我可以这样做:

String orgName = "anyParamValue";

Query q = em.createQuery("Select O from Organization O where O.orgName LIKE '%:orgName%'");

q.setParameter("orgName", orgName);

I don't know if I am late or out of scope but in my opinion I could do it like:

String orgName = "anyParamValue";

Query q = em.createQuery("Select O from Organization O where O.orgName LIKE '%:orgName%'");

q.setParameter("orgName", orgName);
楠木可依 2024-08-10 01:18:22
  1. 使用下面的 JPQL 查询。
select i from Instructor i where i.address LIKE CONCAT('%',:address ,'%')");
  1. 使用以下标准代码进行相同操作:
@Test
public void findAllHavingAddressLike() {
    CriteriaBuilder cb = criteriaUtils.criteriaBuilder();
    CriteriaQuery<Instructor> cq = cb.createQuery(Instructor.class);
    Root<Instructor> root = cq.from(Instructor.class);
    printResultList(cq.select(root).where(
        cb.like(root.get(Instructor_.address), "%#1074%")));
}
  1. Use below JPQL query.
select i from Instructor i where i.address LIKE CONCAT('%',:address ,'%')");
  1. Use below Criteria code for the same:
@Test
public void findAllHavingAddressLike() {
    CriteriaBuilder cb = criteriaUtils.criteriaBuilder();
    CriteriaQuery<Instructor> cq = cb.createQuery(Instructor.class);
    Root<Instructor> root = cq.from(Instructor.class);
    printResultList(cq.select(root).where(
        cb.like(root.get(Instructor_.address), "%#1074%")));
}
撧情箌佬 2024-08-10 01:18:22

使用 JpaRepositoryCrudRepository 作为存储库接口:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    @Query("SELECT t from Customer t where LOWER(t.name) LIKE %:name%")
    public List<Customer> findByName(@Param("name") String name);

}


@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {

    private CustomerRepository customerRepository;
    
    //...

    @Override
    public List<Customer> pattern(String text) throws Exception {
        return customerRepository.findByName(text.toLowerCase());
    }
}

Use JpaRepository or CrudRepository as repository interface:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    @Query("SELECT t from Customer t where LOWER(t.name) LIKE %:name%")
    public List<Customer> findByName(@Param("name") String name);

}


@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {

    private CustomerRepository customerRepository;
    
    //...

    @Override
    public List<Customer> pattern(String text) throws Exception {
        return customerRepository.findByName(text.toLowerCase());
    }
}
橘香 2024-08-10 01:18:22

只需省略 ''

LIKE %:code%

Just leave out the ''

LIKE %:code%
顾挽 2024-08-10 01:18:22

使用 JPQL 查询。

@Query("select e from Entity e where e.id = ?1 and e.code like CONCAT('%', CONCAT(?2, '%'))")
List<Entity> findByIdAndCodeLike(Long id, String code);

Use JPQL query.

@Query("select e from Entity e where e.id = ?1 and e.code like CONCAT('%', CONCAT(?2, '%'))")
List<Entity> findByIdAndCodeLike(Long id, String code);
悍妇囚夫 2024-08-10 01:18:22

只需使用 LIKE %:name%

也许您需要使用 UPPER() 不区分大小写

just use LIKE %:name%

maybe you need to use UPPER() for case insensitive

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