英文:
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);
}
我知道像 findUserByEmailId
和 findUserByPhoneNumber
这样的方法没有在 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<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.
<b>EDIT:</b>
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;
}
答案1
得分: 3
Spring Data JPA可以从方法名称中派生查询。您应该在JPA方法命名查询中使用确切的字段名称和参数。在方法名称中使用 UserEmailId
、UserPhoneNumber
和 UserPassword
。
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<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论