I have two tables represented by two entities :
@Entity
public class HostEntity
{
@NotNull
private String myGroup;
@Id
@NotNull
private String host;
@NotNull
private long inGroupSince;
}
and
@Entity
public class GroupEntity
{
@NotNull
private String groupId;
@Id
@NotNull
private String propertiesStr;
}
I have crudRepository for each entity/table.
So, given two numbers , startTime and finishTime ,and two strings , stringA and stringB first - get all HostEntity.myGroup (lets call this ListA ) such that HostEntity.inGroupSince is between startTime and finishTine, and then , return all GroupEntity.groupId such that GroupEntity.groupdId is in ListA and GroupEntity.propertiesStr containt stringA and StringB
What would be the best way to achieve that ? I can do it in one query ?
Im working in Spring Boot with crudRepository and kinda new to it.
I can use @query annotations in the repostiroy , for example I got the following code :
@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);
}
which return every GroupEntity such that GroupEntity.propertiesStr containts 11 strings inside of it
UPDATE
I used the following as suggested below :
@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);
and I put it inside GroupEntity repository, but its not working . what am I doing wrong ?
[...] get all HostEntity.myGroup (lets call this ListA) such that HostEntity.inGroupSince is between startTime and finishTine [...]
SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime
[...] then , return all GroupEntity.groupId such that GroupEntity.groupdId is in ListA [...]
Use above SELECT as inner select:
SELECT groupId FROM GroupEntity
WHERE
groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime)
[...] and GroupEntity.propertiesStr containt stringA and StringB [...]
Add LIKEs:
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%'
okie !!! got it to work !!
used the following query :
@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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.