簡體   English   中英

使用一個表/實體的查詢結果查詢另一個實體/表

[英]Use result of of query of one table/entity to query another entity/table

我有兩個表,由兩個實體表示:

        @Entity
    public class HostEntity
    {
        @NotNull
        private String myGroup;

        @Id
        @NotNull
        private String host;

        @NotNull
        private long inGroupSince;
}

    @Entity
    public class GroupEntity
    {
        @NotNull
        private String groupId;

        @Id
        @NotNull
        private String propertiesStr;
}

我為每個實體/表都有crudRepository

因此,給定兩個數字startTimefinishTime ,以及兩個字符串stringAstringB首先-獲取所有HostEntity.myGroup(以下稱此ListA ),使HostEntity.inGroupSince在startTime和finishTine之間,然后返回所有GroupEntity.groupId該GroupEntity.groupdId在ListA和GroupEntity.propertiesStr中包含stringA和StringB

實現這一目標的最佳方法是什么? 我可以在一個查詢中做到嗎?

我使用crudRepositorySpring Boot中工作,這是一個新手。

我可以在repostiroy中使用@query批注,例如,我得到了以下代碼:

@Repository
@Profile(Constants.SPRING_PROFILE_DEVELOPMENT)
public interface IGroupsRepositoryDev extends IGroupsRepository
{
    @Query("SELECT j FROM GroupEntity j WHERE LOWER(j.propertiesStr) LIKE %?1%  and LOWER(j.propertiesStr) LIKE %?2% and LOWER(j.propertiesStr) LIKE %?3%"
        + " and LOWER(j.propertiesStr) LIKE %?4% and LOWER(j.propertiesStr) LIKE %?5% and LOWER(j.propertiesStr) LIKE %?6% and LOWER(j.propertiesStr) LIKE %?7%"
        + " and LOWER(j.propertiesStr) LIKE %?8% and LOWER(j.propertiesStr) LIKE %?9% and LOWER(j.propertiesStr) LIKE %?10% and LOWER(j.propertiesStr) LIKE %?11% ")
    List<UUID> findByProperties(String property1,String property2,String property3,String property4,String property5,String property6
        ,String property7,String property8,String property9,String property10,String property11);

}

返回每個GroupEntity,以便GroupEntity.propertiesStr在其中包含11個字符串

UPDATE

我按照以下建議使用以下內容:

    @Query(" SELECT groupId from GroupEntity where groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > ?12 AND inGroupSince < ?13) "
       + "AND LOWER(propertiesStr) LIKE %?1%  and LOWER(propertiesStr) LIKE %?2% and LOWER(propertiesStr) LIKE %?3%"
       + " and LOWER(propertiesStr) LIKE %?4% and LOWER(propertiesStr) LIKE %?5% and LOWER(propertiesStr) LIKE %?6% and LOWER(propertiesStr) LIKE %?7%"
       + " and LOWER(propertiesStr) LIKE %?8% and LOWER(propertiesStr) LIKE %?9% and LOWER(propertiesStr) LIKE %?10% and LOWER(propertiesStr) LIKE %?11% ")
   List<String> findByPropertiesBetweenTime(String property1,String property2,String property3,String property4,String property5,String property6
       ,String property7,String property8,String property9,String property10,String property11,long st,long ft);

我把它放到GroupEntity倉庫中,但是不起作用。 我究竟做錯了什么 ?

[...]獲取所有HostEntity.myGroup(我們稱其為ListA),使得HostEntity.inGroupSince在startTime和finishTine之間[...]
SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime
[...]然后,返回所有GroupEntity.groupId,以使GroupEntity.groupdId位於ListA中。[...]

使用上面的SELECT作為內部選擇:

SELECT groupId FROM GroupEntity 
WHERE 
  groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime)
[...]和GroupEntity.propertiesStr包含stringA和StringB [...]

添加喜歡:

SELECT groupId FROM GroupEntity 
WHERE 
  groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime)
AND propertiesStr LIKE '%stringA%' 
AND propertiesStr LIKE '%stringB%'

好吧! 得到它的工作!

使用以下查詢:

@Query("select ge from GroupEntity ge where ge.groupId in ( select k.myGroup from HostEntity k where k.inGroupSince > ?1 and k.inGroupSince < ?2 ) "
       + "and ge.propertiesStr like %?3% and ge.propertiesStr like %?4% and ge.propertiesStr like %?5% and ge.propertiesStr like %?6% and ge.propertiesStr like %?7% "
       + "and ge.propertiesStr like %?8% and ge.propertiesStr like %?9% and ge.propertiesStr like %?10% and ge.propertiesStr like %?11% and ge.propertiesStr like %?12% "
       + " and ge.propertiesStr like %?13%")
    List<GroupEntity> findGrpWithPropertiesBetweenTime(long st,long ft,String property1,String property2,String property3,String property4,String property5,String property6
       ,String property7,String property8,String property9,String property10,String property11);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM