2017-02-12 51 views
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實體/表。

因此,考慮到兩個數字,開始時間finishTime,和兩個字符串,stringAstringB第一 - 讓所有HostEntity.myGroup(可以稱之爲利斯塔),使得HostEntity.inGroupSince是startTime和finishTine之間,然後,返回所有GroupEntity.groupId這樣GroupEntity.groupdId是利斯塔和GroupEntity.propertiesStr containt stringA和StringB

w ^帽子將是實現這一目標的最佳方式?我可以在一個查詢中做到這一點?

林在春季啓動正與crudRepository還挺新的。

我可以使用@query註釋在repostiroy,比如我下面的代碼:

@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 containts它裏面

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存儲庫中,但它不工作。我究竟做錯了什麼 ?

回答

0
[...]讓所有HostEntity.myGroup(可以稱之爲利斯塔),使得HostEntity.inGroupSince是startTime和finishTine之間[...]
SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime 
[...]然後,返回所有GroupEntity.groupId使得GroupEntity.groupdId是在利斯塔[...]

使用上述SELECT作爲內選擇:

SELECT groupId FROM GroupEntity 
WHERE 
    groupId IN (SELECT myGroup FROM HostEntity WHERE inGroupSince > startTime AND inGroupSince < finishTime) 
[...]和GroupEntity.propertiesStr containt 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%' 
+0

感謝,但在庫我應該把它?它沒有關係? – nadavgam

+0

對不起,我不是那種熟悉Spring Boot的人,但在你的例子中使用上面的@query註釋查詢似乎是合理的。 – hartwecm

0

okie!得到它的工作!

使用下面的查詢:

@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); 
相關問題