无法为该方法创建查询。在Spring Boot中创建自定义查询

huangapple go评论66阅读模式
英文:

Failed to create a query for method. Creating a custom query in springboot

问题

以下是您要翻译的内容:

我是Spring Boot的新手,我正在尝试编写一个登录方法,该方法将根据电子邮件和密码或手机号和密码登录用户。
在我的服务中,我已经写了类似这样的代码:

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

我知道像 findUserByEmailIdfindUserByPhoneNumber 这样的方法没有在 CrudRepository 接口中定义。这就是为什么我会得到这个错误。

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)

我的问题是,我如何在Spring Boot中实现自定义查询?我知道JPQL和本地SQL的实现方式。但这是否是推荐的做法?
假设将来我应该实现搜索功能,现在搜索机制本身就是一个复杂的实现。我可以在搜索机制中实现JPQL或本地SQL吗?或者Spring Boot提供了其他我不知道的方法吗?
我知道在一个帖子中提了很多问题。但是当我试图在互联网上找到确切答案时感到很困惑。

编辑:
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;
}
英文:

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&lt;AuthenticationModel, String&gt; {
    Iterable&lt;AuthenticationModel&gt; findUserByEmailId(String emailId, String password);
    Iterable&lt;AuthenticationModel&gt; 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.&lt;init&gt;(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.

<b>EDIT:</b>
AuthenticationModel.class

public class AuthenticationModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    @NotBlank
    @Column(name = &quot;user_uuid&quot;, unique = true)
    private UUID userUuid;
    @NotNull
    @NotBlank
    @Email
    @Column(name = &quot;user_email_id&quot;, unique = true)
    private String userEmailId;
    @NotNull
    @NotBlank
    @Positive
    @Size(min = 10, max = 10)
    @Column(name = &quot;user_phone_number&quot;, unique = true)
    private Long userPhoneNumber;
    @NotNull
    @Column(name = &quot;user_password&quot;)
    private String userPassword;
}

答案1

得分: 3

Spring Data JPA可以从方法名称中派生查询。您应该在JPA方法命名查询中使用确切的字段名称和参数。在方法名称中使用 UserEmailIdUserPhoneNumberUserPassword

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

您可以在这里查阅有关JPA方法命名查询的详细信息:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

英文:

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&lt;AuthenticationModel&gt; findByUserEmailIdAndUserPassword(String emailId, String password);
List&lt;AuthenticationModel&gt; 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

huangapple
  • 本文由 发表于 2020年7月25日 14:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定