Spring JPA 存储库无法解析 属性 实体
Spring JPA repository cannot resolve property of entity
我正在尝试对 table 的两列进行简单搜索。
我的客户实体 class 片段:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CUSTOMER_ID", nullable = false)
private int customer_id;
@Column(name = "FIRST_NAME", nullable = false)
private String first_name;
@Column(name = "LAST_NAME", nullable = false)
private String last_name;
@Column(name = "PHONE_NUMBER", nullable = false)
private String phone_number;
@Column(name = "EMAIL")
private String email;
我的 customerRepository class 片段:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByFirst_nameAndLast_name(String firstName, String lastname);
}
在编译和 运行 springBoot 时我得到这个错误:
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.owl.owlserver.repositories.CustomerRepository.findAllByFirst_nameAndLast_name(java.lang.String,java.lang.String)! No property first found for type Customer!
所以它无法检测到客户对象中的字段,我应该以某种方式将客户 class 导入存储库吗?
我建议按照标准将属性保存在驼峰式外壳中
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CUSTOMER_ID", nullable = false)
private int customerId;
@Column(name = "FIRST_NAME", nullable = false)
private String firstName;
@Column(name = "LAST_NAME", nullable = false)
private String lastName;
@Column(name = "PHONE_NUMBER", nullable = false)
private String phoneNumber;
@Column(name = "EMAIL")
private String email;
查询
List<Customer> findAllByFirstNameAndLastName(String firstName, String lastname);
问题出在你的snake_case命名规范上,建议使用驼峰命名规范。
正如 Spring 文档所说:
Because we treat the underscore character as a reserved character, we
strongly advise following standard Java naming conventions (that is,
not using underscores in property names but using camel case instead).
您可以看到相关的 JIRA 问题 here -- 属性 名称不支持下划线。
SpringJPA 的重点是使用 Repository 中定义的方法来简化查询构建。约定是使用驼峰式命名实体中的字段,在编写查询方法时也是如此。只需将实体字段的命名从 snake case 更改为 camel case 即可。
我正在尝试对 table 的两列进行简单搜索。
我的客户实体 class 片段:
@Entity
@Table(name = "CUSTOMER")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CUSTOMER_ID", nullable = false)
private int customer_id;
@Column(name = "FIRST_NAME", nullable = false)
private String first_name;
@Column(name = "LAST_NAME", nullable = false)
private String last_name;
@Column(name = "PHONE_NUMBER", nullable = false)
private String phone_number;
@Column(name = "EMAIL")
private String email;
我的 customerRepository class 片段:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByFirst_nameAndLast_name(String firstName, String lastname);
}
在编译和 运行 springBoot 时我得到这个错误:
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.owl.owlserver.repositories.CustomerRepository.findAllByFirst_nameAndLast_name(java.lang.String,java.lang.String)! No property first found for type Customer!
所以它无法检测到客户对象中的字段,我应该以某种方式将客户 class 导入存储库吗?
我建议按照标准将属性保存在驼峰式外壳中
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CUSTOMER_ID", nullable = false)
private int customerId;
@Column(name = "FIRST_NAME", nullable = false)
private String firstName;
@Column(name = "LAST_NAME", nullable = false)
private String lastName;
@Column(name = "PHONE_NUMBER", nullable = false)
private String phoneNumber;
@Column(name = "EMAIL")
private String email;
查询
List<Customer> findAllByFirstNameAndLastName(String firstName, String lastname);
问题出在你的snake_case命名规范上,建议使用驼峰命名规范。
正如 Spring 文档所说:
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
您可以看到相关的 JIRA 问题 here -- 属性 名称不支持下划线。
SpringJPA 的重点是使用 Repository 中定义的方法来简化查询构建。约定是使用驼峰式命名实体中的字段,在编写查询方法时也是如此。只需将实体字段的命名从 snake case 更改为 camel case 即可。