【问题标题】:Spring Rest Repository ErrorSpring Rest 存储库错误
【发布时间】:2017-08-08 08:09:05
【问题描述】:

在 Repositories 中的 Rest Spring API 中,我需要通过三个参数查找值,但即电话、密码、查找 delete=0 查找依据。我已经将它添加到实体中,但它显示错误无效派生查询!找不到类型 CustomerEntity 的属性被删除!两个字段工作正常,但 is-deleted 不起作用

@Repository
public interface CustomerRepository extends JpaRepository<CustomerEntity, Long> {
CustomerEntity findByfacebookID(String facebookID);
CustomerEntity findByPhoneAndPasswordAndIsDeleted(String phone, String password, Boolean isdeleted);

实体

@Entity
@Table(name = "CUSTOMER")
public class CustomerEntity {

    @Column(name = "IS_DELETED", length = 3, nullable = false)
    private Boolean isdeleted;

【问题讨论】:

  • 为什么属性名称中有空格?正好在private Boolean is deleted

标签: java spring rest spring-data-jpa


【解决方案1】:

Spring Data JPA 假定您的字段名称遵循某些约定,以便正确生成派生查询。资本化问题; isdeleted != isDeleted。另外,IsXYZ 在 Spring Data JPA 查询中有一个special meaning,很可能会导致你的语法有歧义。一般来说,遵守约定,事情会简单得多:

@Repository
public interface CustomerRepository extends JpaRepository<CustomerEntity, Long> {
    CustomerEntity findByfacebookID(String facebookID);

    CustomerEntity findByPhoneAndPasswordAndDeleted(String phone, String password, Boolean deleted);
}

@Entity
@Table(name = "CUSTOMER")
public class CustomerEntity {

    @Column(name = "IS_DELETED", length = 3, nullable = false)
    private Boolean deleted;

    // phone, password, other fields...
}

简而言之:从布尔变量中删除 is 前缀(通常用于访问器的方法名称,而不是字段名称)。

【讨论】:

  • 值得注意的是:根据您看到的错误消息,只需在 private Boolean isdeleted; 中大写 d 也可能会解决此问题,但值得进行完整更新以避免在您的查询下一次更改。
  • 感谢 nbrooks,它的工作是肯定的,这是 d 的问题。我必须在小型大写字母中使用它已删除
猜你喜欢
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 2017-08-23
  • 1970-01-01
  • 2020-07-23
  • 2017-10-18
相关资源
最近更新 更多