Skip to content

Commit e3c2f29

Browse files
EFRS-1350: Refactored repositories to fit Java 17
1 parent 6311724 commit e3c2f29

File tree

3 files changed

+105
-64
lines changed

3 files changed

+105
-64
lines changed

java/common/src/main/java/com/exadel/frs/commonservice/repository/EmbeddingRepository.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
public interface EmbeddingRepository extends JpaRepository<Embedding, UUID> {
1919

2020
// Note: consumer should consume in transaction
21-
@Query("select " +
22-
" new com.exadel.frs.commonservice.projection.EnhancedEmbeddingProjection(e.id, e.embedding, s.subjectName)" +
23-
" from " +
24-
" Embedding e " +
25-
" left join " +
26-
" e.subject s " +
27-
" where " +
28-
" s.apiKey = :apiKey")
21+
@Query("""
22+
select
23+
new com.exadel.frs.commonservice.projection.EnhancedEmbeddingProjection(e.id, e.embedding, s.subjectName)
24+
from
25+
Embedding e
26+
left join
27+
e.subject s
28+
where
29+
s.apiKey = :apiKey
30+
""")
2931
Stream<EnhancedEmbeddingProjection> findBySubjectApiKey(@Param("apiKey") String apiKey);
3032

3133
@EntityGraph("embedding-with-subject")
@@ -52,43 +54,54 @@ int updateEmbedding(@Param("embeddingId") UUID embeddingId,
5254
@Query("update Embedding e set e.subject = :toSubject where e.subject = :fromSubject")
5355
int reassignEmbeddings(@Param("fromSubject") Subject fromSubject, @Param("toSubject") Subject toSubject);
5456

55-
@Query("select " +
56-
" new com.exadel.frs.commonservice.projection.EmbeddingProjection(e.id, e.subject.subjectName)" +
57-
" from " +
58-
" Embedding e " +
59-
" where " +
60-
" e.subject.apiKey = :apiKey")
57+
@Query("""
58+
select
59+
new com.exadel.frs.commonservice.projection.EmbeddingProjection(e.id, e.subject.subjectName)
60+
from
61+
Embedding e
62+
where
63+
e.subject.apiKey = :apiKey
64+
""")
6165
Page<EmbeddingProjection> findBySubjectApiKey(String apiKey, Pageable pageable);
6266

63-
@Query("select " +
64-
" new com.exadel.frs.commonservice.projection.EmbeddingProjection(e.id, e.subject.subjectName)" +
65-
" from " +
66-
" Embedding e " +
67-
" where " +
68-
" e.subject.apiKey = :apiKey" +
69-
" and (cast(:subjectName as string) is null or e.subject.subjectName = :subjectName)")
67+
@Query("""
68+
select
69+
new com.exadel.frs.commonservice.projection.EmbeddingProjection(e.id, e.subject.subjectName)
70+
from
71+
Embedding e
72+
where
73+
e.subject.apiKey = :apiKey
74+
and
75+
(cast(:subjectName as string) is null or e.subject.subjectName = :subjectName)
76+
""")
7077
Page<EmbeddingProjection> findBySubjectApiKeyAndSubjectName(String apiKey, String subjectName, Pageable pageable);
7178

7279
@Query("select distinct(e.calculator) from Embedding e")
7380
List<String> getUniqueCalculators();
7481

75-
@Query("select " +
76-
" count(e) " +
77-
" from " +
78-
" Embedding e " +
79-
" where " +
80-
" e.subject.apiKey = :apiKey " +
81-
" and e.calculator <> :calculator")
82+
@Query("""
83+
select
84+
count(e)
85+
from
86+
Embedding e
87+
where
88+
e.subject.apiKey = :apiKey
89+
and
90+
e.calculator <> :calculator
91+
""")
8292
Long countBySubjectApiKeyAndCalculatorNotEq(@Param("apiKey") String apiKey,
8393
@Param("calculator") String calculator);
8494

85-
@Query("select " +
86-
" count(e) " +
87-
" from " +
88-
" Embedding e " +
89-
" where " +
90-
" e.subject.apiKey <> :apiKey " +
91-
" and e.calculator <> :calculator")
95+
@Query("""
96+
select
97+
count(e)
98+
from
99+
Embedding e
100+
where
101+
e.subject.apiKey <> :apiKey
102+
and
103+
e.calculator <> :calculator
104+
""")
92105
Long countBySubjectApiKeyNotEqAndCalculatorNotEq(@Param("apiKey") String apiKey,
93106
@Param("calculator") String calculator);
94107
}

java/common/src/main/java/com/exadel/frs/commonservice/repository/ModelRepository.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,70 @@
1717
package com.exadel.frs.commonservice.repository;
1818

1919
import com.exadel.frs.commonservice.entity.Model;
20+
import com.exadel.frs.commonservice.enums.ModelType;
2021
import com.exadel.frs.commonservice.projection.ModelProjection;
2122
import com.exadel.frs.commonservice.projection.ModelSubjectProjection;
22-
import com.exadel.frs.commonservice.enums.ModelType;
23+
import java.util.List;
24+
import java.util.Optional;
2325
import java.util.Set;
2426
import java.util.stream.Stream;
2527
import org.springframework.data.jpa.repository.JpaRepository;
2628
import org.springframework.data.jpa.repository.Query;
2729
import org.springframework.stereotype.Repository;
2830

29-
import java.util.List;
30-
import java.util.Optional;
31-
3231
@Repository
3332
public interface ModelRepository extends JpaRepository<Model, Long> {
33+
3434
Optional<Model> findByApiKeyAndType(String apiKey, ModelType type);
3535

3636
Stream<Model> findAllByIdIn(Set<Long> ids);
3737

3838
Optional<Model> findByGuid(String guid);
3939

40-
@Query("select case when count(m) > 0 then TRUE else FALSE end " +
41-
"from Model m " +
42-
"where lower(m.name) = lower(:name) AND m.app.id = :appId")
40+
@Query("""
41+
select
42+
case when count(m) > 0 then TRUE else FALSE end
43+
from
44+
Model m
45+
where
46+
lower(m.name) = lower(:name)
47+
and
48+
m.app.id = :appId
49+
""")
4350
boolean existsByUniqueNameAndAppId(String name, Long appId);
4451

45-
@Query("select count(m) " +
46-
"from Model m " +
47-
"where lower(m.name) = lower(:name) AND m.app.id = :appId")
52+
@Query("""
53+
select
54+
count(m)
55+
from
56+
Model m
57+
where
58+
lower(m.name) = lower(:name)
59+
and
60+
m.app.id = :appId
61+
""")
4862
int countByUniqueNameAndAppId(String name, Long appId);
4963

50-
@Query("SELECT " +
51-
" new com.exadel.frs.commonservice.projection.ModelSubjectProjection(m.guid, count(s.id)) " +
52-
" FROM " +
53-
" Model m LEFT JOIN Subject s ON m.apiKey = s.apiKey " +
54-
" GROUP BY " +
55-
" m.guid")
64+
@Query("""
65+
select
66+
new com.exadel.frs.commonservice.projection.ModelSubjectProjection(m.guid, count(s.id))
67+
from
68+
Model m
69+
left join
70+
Subject s on m.apiKey = s.apiKey
71+
group by
72+
m.guid
73+
""")
5674
List<ModelSubjectProjection> getModelSubjectsCount();
5775

5876
@Query("""
59-
SELECT DISTINCT
60-
new com.exadel.frs.commonservice.projection.ModelProjection(
61-
m.guid, m.name, m.apiKey, m.type, m.createdDate
62-
)
63-
FROM
64-
Model m LEFT JOIN m.app a
65-
WHERE
77+
select distinct
78+
new com.exadel.frs.commonservice.projection.ModelProjection(m.guid, m.name, m.apiKey, m.type, m.createdDate)
79+
from
80+
Model m
81+
left join
82+
m.app a
83+
where
6684
a.id = :appId
6785
""")
6886
List<ModelProjection> findAllByAppId(Long appId);

java/common/src/main/java/com/exadel/frs/commonservice/repository/ModelStatisticRepository.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ public interface ModelStatisticRepository extends JpaRepository<ModelStatistic,
1616

1717
Stream<ModelStatistic> findAllByModelIdInAndCreatedDate(Set<Long> modelIds, LocalDateTime createdDate);
1818

19-
@Query("SELECT new com.exadel.frs.commonservice.projection.ModelStatisticProjection(SUM(statistic.requestCount), CAST(statistic.createdDate AS date)) " +
20-
"FROM ModelStatistic AS statistic " +
21-
"JOIN statistic.model AS model " +
22-
"WHERE model.guid = :modelGuid AND CAST(statistic.createdDate AS date) BETWEEN :startDate AND :endDate " +
23-
"GROUP BY CAST(statistic.createdDate AS date) " +
24-
"ORDER BY CAST(statistic.createdDate AS date) DESC")
19+
@Query("""
20+
select
21+
new com.exadel.frs.commonservice.projection.ModelStatisticProjection(sum(statistic.requestCount), cast(statistic.createdDate as date))
22+
from
23+
ModelStatistic as statistic
24+
join
25+
statistic.model as model
26+
where
27+
model.guid = :modelGuid
28+
and
29+
cast(statistic.createdDate as date) between :startDate and :endDate
30+
group by
31+
cast(statistic.createdDate as date)
32+
order by
33+
cast(statistic.createdDate as date) desc
34+
""")
2535
List<ModelStatisticProjection> findAllSummarizedByDay(String modelGuid, Date startDate, Date endDate);
2636
}

0 commit comments

Comments
 (0)