0

I am new to springboot, and I am trying to write a login method which will login user based on emailid and password or phone number and password. In my service I have written something like this

public interface AuthService extends CrudRepository<AuthenticationModel, String> {
    Iterable<AuthenticationModel> findUserByEmailId(String emailId, String password);
    Iterable<AuthenticationModel> findUserByPhoneNumber(Long phoneNumber, String password);
}

I know methods like findUserByEmailId and findUserByPhoneNumber are not written in CrudRepository interface. That's why I am getting this error.

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Iterable com.xyz.services.authenticate.AuthService.findUserByPhoneNumber(java.lang.Long,java.lang.String)! No property phoneNumber found for type AuthenticationModel!
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96)

What my concern is how can I implement my custom query in springboot ? I am aware of JPQL and native SQL implementations. But Is this recommended way of doing this ? Say In future I am supposed to implement the search functionality, now search mechanism is itself a huge implementation. Can I implement the JPQL or native SQL implementation in search mechanism ? Or Is there another way which springboot provides which I am not aware of ?

I know there are lot of questions in one post. But I am confused when I tried to drill down to the actual answer on internet.

EDIT: AuthenticationModel.class

public class AuthenticationModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    @NotBlank
    @Column(name = "user_uuid", unique = true)
    private UUID userUuid;
    @NotNull
    @NotBlank
    @Email
    @Column(name = "user_email_id", unique = true)
    private String userEmailId;
    @NotNull
    @NotBlank
    @Positive
    @Size(min = 10, max = 10)
    @Column(name = "user_phone_number", unique = true)
    private Long userPhoneNumber;
    @NotNull
    @Column(name = "user_password")
    private String userPassword;
}
5
  • you can use @query notation to over your method and write complex queries for instance @Query("SELECT u FROM User u WHERE u.status = 1") Collection<User> findAllActiveUsers(); because writing long method name might not help you and is not recomended Commented Jul 25, 2020 at 7:02
  • Can you show us AuthenticationModel ? Commented Jul 25, 2020 at 7:04
  • @User-Upvotedon'tsayThanks, that's a entity model having uuid, email, phone, and password Commented Jul 25, 2020 at 7:06
  • Please add the model in post. Commented Jul 25, 2020 at 7:07
  • @User-Upvotedon'tsayThanks, added. Can you tell me how that's going to help to answer my question ? Commented Jul 25, 2020 at 7:15

1 Answer 1

3

Spring data JPA derived query from the method name. You should use exact field name and parameter also for JPA method naming query. Here use UserEmailId, UserPhoneNumber and UserPassword in method name

List<AuthenticationModel> findByUserEmailIdAndUserPassword(String emailId, String password);
List<AuthenticationModel> findByUserPhoneNumberAndUserPassword(Long phoneNumber, String password);

Here you can read details about JPA method naming query https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

Sign up to request clarification or add additional context in comments.

3 Comments

oh!, it compiled without any issue ? I want to understand this, what happened ? Does the springboot form a query depending on the method name we give ? I am just running to see if this is working fine
The JPA module supports defining a query manually as a String or having it being derived from the method name. So from method name it derived the query automatically I added a doc about this in answer
I won't say thanks, I will upvote and accept your answer + Thanks :)

Your Answer

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.