【问题标题】:InvalidDataAccessApiUsageException: Parameter value element did not match expected typeInvalidDataAccessApiUsageException:参数值元素与预期类型不匹配
【发布时间】:2016-11-05 17:34:54
【问题描述】:

我正在尝试使用 Spring Data 执行 IN 查询。我的模型如下所示:

@Entity
@Table(name = "customer", schema = "public", catalog = "postgres")
public class CustomerEntity {
    private int id;
    private String name;
    private int balance;
    private String bankId;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "balance")
    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Basic
    @Column(name = "bank_id")
    public String getBankId() {
        return bankId;
    }

    public void setBankId(String bankId) {
        this.bankId = bankId;
    }

我的存储库界面如下所示:

@Repository
public interface TransactionsRepository extends JpaRepository<TransactionsEntity, Long> {

    List<TransactionsEntity> findByCustomerIdIn(List<CustomerEntity> customerEntities);

}

问题是当我尝试执行这段代码时 List&lt;TransactionsEntity&gt; transactionsEntitiesList = transactionsRepository.findByCustomerIdIn(customerEntitiesList);

我得到了这个例外:

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value element [org.example.domain.admin.CustomerEntity@6a1a2a4] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [org.example.domain.admin.CustomerEntity@6a1a2a4] did not match expected type [java.lang.String (n/a)]

更新:TransactionsEntity.class

@Entity
@Table(name = "transactions", schema = "public", catalog = "postgres")
public class TransactionsEntity {

    private String id;
    private String amount;
    private String customerId;

    @Id
    @Column(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "amount")
    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    @Basic
    @Column(name = "customer_id")
    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TransactionsEntity that = (TransactionsEntity) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (amount != null ? !amount.equals(that.amount) : that.amount != null) return false;
        if (customerId != null ? !customerId.equals(that.customerId) : that.customerId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (amount != null ? amount.hashCode() : 0);
        result = 31 * result + (customerId != null ? customerId.hashCode() : 0);
        return result;
    }
}

【问题讨论】:

  • 您也可以发布您的 TransationEntity 吗?
  • 我已经更新了原帖。同时我已经解决了这个问题,我将一个对象传递给查询,但它需要一个字符串。我想我对 spring-data 给予了太多的信任 :) 我对结果不满意,因为我首先得到了一个客户列表,然后我必须创建一个单独的客户 ID(字符串)值列表并使用该列表进行查询.也许还有其他方法?

标签: java spring-boot spring-data spring-data-jpa


【解决方案1】:

正如它在异常中所说的那样,Spring 需要一个 String,因为您的 TransactionEntity 中的 customer_id 是一个字符串,但您输入的是一个 CustomerEntity。相反,您应该输入带有客户 ID 列表的 List&lt;String&gt;

顺便说一句,假设您将id 设置为CustomerEntityid,您的customer_id 不应该是int 吗?

然后你可以做类似的事情

List<Integer> customerIds = customerEntitiesList.stream().map(CustomerEntity::getId).collect(Collectors.toList());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 2020-06-22
    • 2013-09-18
    • 2014-06-09
    • 2019-03-13
    • 2016-07-13
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多