creating Spring JPARepository method for entity variable with name "cId"

Ashish Parab

I have entity class as

class Policy
{
private String customerId;

private String sicNumber;

private String cId;
/*
more variables can be ignored
*/
}

now inside JPA Repository interface, I want to create method that finds based on sicNumber And cId

following is the attempt i tried

Policy findBySicNumberAndCId(String policyNumber, String cId) ; 

above method asks spring to search for "CId" property in entity class

likewise i tried almost all combinations

Policy findBySicNumberAndcId(String policyNumber, String cId) ; 

above method caused spring to look for "AndcId".

now the question is what should be the method name that will target right property in entity class.

thanks in advance

Init_Rebel

The issue is with the way Spring Data JPA forms the query out of the method name. It follows the regular camel casing approach where the field names are inferred. As per the convention followed, if a method name has uppercase letters consecutively, the field name is inferred in uppercase.

So in your case, findBySicNumberAndCId - the field inferred is CId & is failing to find it. One way to overcome the problem is having the method name with CId in the beginning with 'c' being in lowercase i.e., findBycIdAndSicNumber .

Alternatively, you can consider renaming the variable to avoid these issues or make use of @Query & define your queries if renaming the variables is not possible.

More info about the issue : https://github.com/spring-projects/spring-data-commons/issues/1996

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JpaRepository method name with DISTINCT ON

Stub of the spring JpaRepository method

Spring JpaRepository using generic entity

Spring JpaRepository - Detach and Attach entity

JPARepository, name method to return field with "text" included

Spring Data JPA: Named method without JpaRepository

Pageable and @Param in a spring data JpaRepository method issue

Regarding Spring JpaRepository method thread safety

Spring boot overiding save Method in JpaRepository interface

Creating variable arity method

Get name from variable in calling method - creating calling method signature, parameters and values

Spring jpa changes entity boolean variable name when return entity object

How can I make a Spring Boot JpaRepository repository method pegeable?

Null pointer Error when accessing JPARepository method in Spring data jpa

spring-data JpaRepository save() method side effect

Pageable and @Param in a spring data JpaRepository method issue [2]

Hibernate @Filter does not work with Spring JpaRepository.findById method

Creating a dynamic variable name in JS

invalid name - creating new variable

JpaRepository not updating entity

JpaRepository not saving entity

Save Entity Using JPARepository

Javascript - variable for method name?

creating a variable with name as same as the name of items in list

Spring Boot: "Error creating bean with name baseConfig: Invocation of init method failed" NPE

Is it possile creating variable without variable name?

Does Spring Data JPA have any way to sum an entity column using method name resolving?

Spring Autowiring by variable name

@Entity is not creating a Table in my database in Spring