InvalidDataAccessApiUsageException: Parameter value element did not match expected type
As it says in the exception Spring expects a String
because your customer_id
in your TransactionEntity
is a String, but you are inputting a CustomerEntity
. Instead you should input a List<String>
with the list of your customer ids.
Btw shouldn't your customer_id
be an int
assuming you set it to the id
of your CustomerEntity
?
Then you could do something like
List<Integer> customerIds = customerEntitiesList.stream().map(CustomerEntity::getId).collect(Collectors.toList());
Deniss M.
I'm just a newb trying to get it together. It is quite hard to do it in a sense that I needs some time to do it.
Updated on June 09, 2022Comments
-
Deniss M. almost 2 years
I'm trying to execute an IN query with by using Spring Data. My model looks like this:
@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; }
And my repository interface looks like this:
@Repository public interface TransactionsRepository extends JpaRepository<TransactionsEntity, Long> { List<TransactionsEntity> findByCustomerIdIn(List<CustomerEntity> customerEntities);
}
The problem is that when I try to execute this code
List<TransactionsEntity> transactionsEntitiesList = transactionsRepository.findByCustomerIdIn(customerEntitiesList);
I get this exception:
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)]
Update: 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; } }