使用一个表/实体的查询结果来查询另一个实体/表

时间:2017-02-12 17:05:08

标签: mysql sql spring spring-boot jpa-2.0

我有两个表由两个实体代表:

        @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

所以,给定两个数字, startTime finishTime ,首先是两个字符串, stringA stringB - 获取所有HostEntity.myGroup(让我们称之为 ListA ),使HostEntity.inGroupSince位于startTime和finishTine之间,然后返回所有GroupEntity.groupId,使GroupEntity.groupdId位于 ListA 和GroupEntity.propertiesStr包含stringA和StringB

实现这一目标的最佳方法是什么?我可以在一个查询中执行此操作吗?

我使用 crudRepository Spring 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个字符串

更新

我使用了以下建议:

    @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存储库中,但它不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:0)

[...]获取所有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 [...]

添加LIKE:

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%'

答案 1 :(得分:0)

好吧!!!让它工作!!

使用以下查询:

@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);