Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 {
    @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;
                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
– henrycharles
                Jul 25, 2020 at 7:02
                @User-Upvotedon'tsayThanks, that's a entity model having uuid, email, phone, and password
– Lokesh Pandey
                Jul 25, 2020 at 7:06
                @User-Upvotedon'tsayThanks, added. Can you tell me how that's going to help to answer my question ?
– Lokesh Pandey
                Jul 25, 2020 at 7:15

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

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 – Lokesh Pandey Jul 25, 2020 at 7:23 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 – Eklavya Jul 25, 2020 at 7:27

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.