简体   繁体   English

jpql左联接获取未返回类似结果

[英]jpql left join fetch not returning results for like

In a spring mvc app using hibernate and MySQL, I have written the following query method to return a list of names with patients: 在使用hibernate和MySQL的spring mvc应用程序中,我编写了以下查询方法以返回带有患者的姓名列表:

@SuppressWarnings("unchecked")
public Collection<Person> findPersonByLastName(String ln) throws DataAccessException{
    Query query = this.em.createQuery("SELECT DISTINCT pers FROM rimPerson pers left join fetch pers.names nm WHERE nm.family LIKE :lnm");
    query.setParameter("lnm", ln);
    return query.getResultList();
}

This is producing the following hibernate sql: 这将产生以下休眠SQL:

Hibernate: 
select distinct
 person0_.hppid as hppid1_340_0_,
 names1_.HJID as HJID1_89_1_,
 person0_2_.classCode_HJID as classCod2_339_0_,
 person0_1_.administrativeGenderCode_HJID as administ2_341_0_,
 person0_1_.birthTime_HJID as birthTim3_341_0_,
 names1_.DELIMITER_ as DELIMITE2_89_1_,
 names1_.FAMILY as FAMILY3_89_1_,
 names1_.named_entity_hppid as named5_89_1_,
 names1_.SUFFIX as SUFFIX4_89_1_,
 names1_.name_entity_HJID as name9_340_0__,
 names1_.HJID as HJID1_89_0__
 from
 rim_person person0_ inner join rim_living_subject person0_1_ on person0_.hppid=person0_1_.hppid
 inner join rim_entity person0_2_ on person0_.hppid=person0_2_.hppid
 inner join rim_infrastructure_root person0_3_ on person0_.hppid=person0_3_.hppid
 left outer join EN names1_ on person0_.hppid=names1_.name_entity_HJID
 where names1_.FAMILY like ?

When I call the above jpql method with the following command, it returns zero results: 当我使用以下命令调用上述jpql方法时,它返回零结果:

this.myappService.findPersonByLastName("");  

I also get zero results when I cut and past the above generated hibernate code into the MySQL command line client and replace ? 当将上面生成的休眠代码剪切并粘贴到MySQL命令行客户端并替换时,我也得到零结果? with '' . ''

If, however, I remove the where names1_.FAMILY like ? 但是,如果我删除where names1_.FAMILY like ? from the hibernate generated sql above and place the shortened sql into the MySQL command line client, I get four results, eachof which has a value for the lastname field. 从上面的休眠生成的sql并将缩短的sql放入MySQL命令行客户端,我得到了四个结果,每个结果都有一个用于lastname字段的值。

How can I change the jpql so that it generates a hibernate query that returns the four results when `` is passed as the empty string parameter? 如何更改jpql,以便生成一个休眠查询,当将``作为空字符串参数传递时,它返回四个结果? I want the result set to include every result when the user gives empty input, but to give filtered results when the user types in any given text input. 我希望结果集在用户提供空输入时包括所有结果,但是在用户键入任何给定的文本输入时提供过滤后的结果。

The typical reason that like fails to do what you think it ought to do is to forget to put a wildcard in the pattern string. like未能完成您认为应该做的事情的典型原因是忘记在模式字符串中放入通配符。 For example, if you want to match all user names that begin with 'Code' you must do something like name like 'Code%' , NOT name like 'Code' . 例如,如果要匹配以'Code'开头的所有用户名,则必须执行name like 'Code%'类的name like 'Code%' ,而不是执行name like 'Code' You can control exactly what your predicate matches with careful placement of % s in your string. 您可以通过在字符串中小心地放置% s来精确控制谓词匹配的内容。

Try this to see all entities no matter what the value in family: 尝试使用此方法查看所有实体,而不管家庭的价值如何:

this.myappService.findPersonByLastName("%");

It is kinda cheesy to have the caller of findPersionByLastName have to put in the % wildcard. findPersionByLastName的调用者必须放入%通配符有点俗气。 A better implementation is to have the caller specify which last name they are looking for, and then have the code that constructs the query put the wildcard in the right place. 一个更好的实现是让调用者指定他们要查找的姓​​氏,然后将构造查询的代码放在正确的位置。 When you are looking for last names, you might do something like this: 当您寻找姓氏时,您可以执行以下操作:

query.setParameter("lnm", "%" + ln);

That would match anything that ends with the parameter that is passed to the method. 这将匹配任何以传递给方法的参数结尾的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM