jpa 中 in 语句的写法

[b]一) in (List<Long>)[/b]

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


[b]二) in(Long[])[/b]原生的 JPA可以如下:
public List<FamilyMember> 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<TimePush> 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 <?3 ")
public List<TimePush> 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<Tweet> 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<TimePush> findPageByTimePush3(String title, String keyWord);

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

public List<TimePush> 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<ServeArea> findDistinctManagerIdByIdsAndManagerType(String ids, Integer manager_type);



五.注意
1)JPA 原生的那种sql,不可以用String startTime去比较数据库中的Calendar startTime。
而@Query(nativeQuery = true,sql="你的sql语句")这种可以。
2)[color=red]Page[/color]<TimePush>的形式好像不能用@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 ")
3)
[table]
||in (List)| in (Array)|in (select) |
|JPA(不需要写sql)|ok|ok|--|
|原生sql|ok|不行|不行|
|nativeQuery sql|ok|未试验|未试验|
[/table]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值