jpa 中 in 语句的写法

[b]一) in (List)[/b]

@Modifying
@Query("DELETE FROM Height h WHERE h.user.id = ?1 AND h.id in (?2)")
public void deleteHeightInBatchByUserIdAndIdIn(Long uid, List hids);


[b]二) in(Long[])[/b]原生的 JPA可以如下:
public List findByFamilyIdAndUserIdIn(Long fid, Long[] uids);


代码试验一(成功):

Long[] adArray = new Long[ads.size()];
for (int i = 0; i < ads.size(); i++) {
adArray[i] = ads.get(i).getId();
}

timePushDao.findByIdIn(adArray);


public List findByIdIn(Long[] adIds);


代码试验二(没成功,估计是不能用@Query("")的形式。)

Long[] adArray = new Long[ads.size()];
for (int i = 0; i < ads.size(); i++) {
adArray[i] = ads.get(i).getId();
}



@Modifying (有这句或者没这句都不行)
@Query("select t from TimePush t where t.ad.id in ?1 and t.pushTime>?2 and t.pushTime public List findPageByTimePush2(Long[] adIds, Calendar startTime, Calendar endTime);


[b]三)in (select) [/b]

@Query("SELECT t FROM Tweet t where t.user.id IN " +
"(SELECT fm1.user.id FROM FamilyMember fm1 where fm1.family.id IN " +
"(SELECT fm2.family.id FROM FamilyMember fm2 where fm2.user.id = ?1)) " +
"AND t.id < ?2")
public Page findFamiliesTweetsByIdLessThan(Long uid, Long tid, Pageable page);


代码试验一(没有成功,很困惑;把Ad改成全路径也不行。)

@Query("select t from TimePush t where t.ad.id in (select a.id form Ad a where a.title like ?1 and a.keyWord like ?2) ")
public List findPageByTimePush3(String title, String keyWord);

上面代码没有成功,换了以下方案,这个方案也更好,其实不需要用in。

public List findByAdTitleLikeAndAdKeyWordLike(String title, String keyWord);

[b]四)拼装String,即“2,3,4” 这种怀疑不行[/b]

@Query(nativeQuery = true,
value = "select * from t_serve_area where addr_id in (?1) and manager_type = ?2 group by manager_id")
public List findDistinctManagerIdByIdsAndManagerType(String ids, Integer manager_type);



五.注意
1)JPA 原生的那种sql,不可以用String startTime去比较数据库中的Calendar startTime。
而@Query(nativeQuery = true,sql="你的sql语句")这种可以。
2)[color=red]Page[/color]的形式好像不能用@Query([color=red]nativeQuery = true[/color],sql="你的sql语句")的语句,能用@Query("select t from TimePush t where t.ad in (?1) and t.pushTime>?2 and t.pushTime 3)
[table]
||in (List)| in (Array)|in (select) |
|JPA(不需要写sql)|ok|ok|--|
|原生sql|ok|不行|不行|
|nativeQuery sql|ok|未试验|未试验|
[/table]

你可能感兴趣的:(jpa)