0

i'm using spring-data-jdbc and i want to store java object as a json into database and when i find or search in the repository i use normal _ as we do in normal spring-data-jdbc nested object as an example findAllByCommonInfo_IsActive(boolean isActive) to search inside object called commonInfo, this is the following representation that i want to apply my question to it

1- What i Want

LuCallingRequestCase.java


@Data
@Builder
@Table
@NoArgsConstructor
@AllArgsConstructor
public class LuCallingRequestCase implements Serializable {
 @Id
 private Long luCallingRequestCaseId;
 private Long luProsecutionWorkRequestTypeId;
 private CommonInfo commonInfo;
}

CommonInfo.java

@Data
@Builder
public class CommonInfo {
 @NonNull
 private final String createdBy;
 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+2")
 private final Instant createdAt;
 private String updatedBy;
 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+2")
 private Instant updatedAt;
 @Builder.Default
 private boolean isActive = true;
 @Builder.Default
 private boolean isUsed = false;
 @Builder.Default
 @JsonProperty("isDeleted")
 private boolean isDeleted = false;
}

Oracle SQL Table


CREATE TABLE LU_CALLING_REQUEST_CASE
(
 LU_CALLING_REQUEST_CASE_ID NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL,
 LU_PROSECUTION_WORK_REQUEST_TYPE_ID NUMBER NOT NULL,
 COMMON_INFO JSON DEFAULT '{
 "createdBy": null,
 "updatedBy": null,
 "createdAt": null,
 "updatedAt": null,
 "isActive": true,
 "isUsed": false,
 "isDeleted": false
 }',
 CONSTRAINT FK4_PROSECUTION_WORK_REQUEST_TYPE FOREIGN KEY (LU_PROSECUTION_WORK_REQUEST_TYPE_ID) REFERENCES LU_PROSECUTION_WORK_REQUEST_TYPE (LU_PROSECUTION_WORK_REQUEST_TYPE_ID) ON DELETE CASCADE
);

2- Exiting Implementation of Repository Layer

LuCallingRequestCaseRepository.java


@Repository
public interface LuCallingRequestCaseRepository extends PagingAndSortingRepository<LuCallingRequestCase, Long>, CrudRepository<LuCallingRequestCase, Long> {
 // Query to count records based on available filter without using 'isDeleted'
 @Query("""
 SELECT COUNT(*)
 FROM LU_CALLING_REQUEST_CASE
 WHERE ( LU_PROSECUTION_WORK_REQUEST_TYPE_ID = :prosecutionWorkTypeId OR :prosecutionWorkTypeId IS NULL )
 """)
 int countByFilter(Long prosecutionWorkTypeId);
 // Query to find records with pagination
 @Query("""
 SELECT 
 LU_CALLING_REQUEST_CASE_ID,
 LU_PROSECUTION_WORK_REQUEST_TYPE_ID,
 JSON_SERIALIZE(LU_PROSECUTION_WORK_REQUEST_TYPE_IDS) AS LU_PROSECUTION_WORK_REQUEST_TYPE_IDS,
 JSON_SERIALIZE(LU_CASE_TYPE_IDS) AS LU_CASE_TYPE_IDS
 FROM LU_CALLING_REQUEST_CASE
 WHERE ( LU_PROSECUTION_WORK_REQUEST_TYPE_ID = :prosecutionWorkTypeId OR :prosecutionWorkTypeId IS NULL )
 ORDER BY LU_CALLING_REQUEST_CASE_ID
 OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY
 """)
 List<LuCallingRequestCase> findByFilter(
 @Param("offset") int offset,
 @Param("limit") int limit,
 Long prosecutionWorkTypeId);
 // Query to find a specific record by LU_CALLING_REQUEST_CASE_ID
 @Query("""
 SELECT 
 LU_CALLING_REQUEST_CASE_ID,
 LU_PROSECUTION_WORK_REQUEST_TYPE_ID,
 JSON_SERIALIZE(LU_PROSECUTION_WORK_REQUEST_TYPE_IDS) AS LU_PROSECUTION_WORK_REQUEST_TYPE_IDS,
 JSON_SERIALIZE(LU_CASE_TYPE_IDS) AS LU_CASE_TYPE_IDS
 FROM LU_CALLING_REQUEST_CASE
 WHERE LU_CALLING_REQUEST_CASE_ID = :callingRequestCaseId
 """)
 Optional<LuCallingRequestCase> findByCallingRequestCaseId(
 @Param("callingRequestCaseId") Long callingRequestCaseId);
}

How Can optimize the repo to be with less code structure in the repo layer instead of dense sql code according to store java object as json and make the framework undertand as an example findAllByCommonInfo_IsActive(boolean isActive) and it search into all records and check the isActive to be true of false from common info json ?

asked Dec 23, 2024 at 13:13

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.