2017-02-12 5 views
0

Ich habe zwei Tabellen, die von zwei Einheiten dargestellt:Verwenden Ergebnis der Abfrage einer Tabelle/Unternehmen ein anderes Unternehmen/Tabelle abzufragen

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

     @Id 
     @NotNull 
     private String host; 

     @NotNull 
     private long inGroupSince; 
} 

und

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

     @Id 
     @NotNull 
     private String propertiesStr; 
} 

Ich habe crudRepository für jede Entität/Tabelle So

, da zwei Zahlen, starttime und finishTime und zwei Saiten, stringA und stringB ersten - erhalten alle HostEntity.myGroup (nennen wir diese ListA), so dass HostEntity.inGroupSince ist zwischen Startzeit und finishTine, und dann kehren alle GroupEntity.groupId so dass GroupEntity.groupdId in ListA und GroupEntity.propertiesStr containt stringA und stringB

W

ist Hut wäre der beste Weg, das zu erreichen? Ich kann es in einer Abfrage tun?

Arbeits Im in Frühling Stiefel mit crudRepository und irgendwie neu zu.

Ich kann @query Anmerkungen in der repostiroy verwenden, zum Beispiel habe ich den folgenden 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); 

} 

die jeden GroupEntity zurückkehren, so dass GroupEntity.propertiesStr containts 11 Saiten in der es

UPDATE

habe ich die folgenden, wie vorgeschlagen unter:

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

und ich legte es in GroupEntity Repository, aber es funktioniert nicht. Was mache ich falsch ?

Antwort

0
[...] erhalten alle HostEntity.myGroup (lässt diese ListA nennen), so dass HostEntity.inGroupSince zwischen Startzeit und finishTine ist [...]
SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime 
[...] dann, Rückkehr Alle GroupEntity.groupId so dass GroupEntity.groupdId in ListA ist [...]

Verwenden oben SELECT als innere select:

SELECT groupId FROM GroupEntity 
WHERE 
    groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime) 
[...] und GroupEntity.propertiesStr containt stringA und stringB [. ..]

hinzufügen Liebt:

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

danke, aber in dem Repository soll ich sagen? es ist egal? – nadavgam

+0

Sorry, ich bin nicht so vertraut mit Spring Boot, aber die obige Abfrage mit der @query Annotation wie in Ihrem Beispiel scheint plausibel. – hartwecm

0

okie !!! habe es geschafft zu arbeiten !!

verwendet die folgende Abfrage:

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