2016-11-05 16 views
1

私は、Springデータを使用してINクエリを実行しようとしています。私のモデルは次のようになります。InvalidDataAccessApiUsageException:パラメータ値の要素が予想される型と一致しません。

@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<TransactionsEntity> transactionsEntitiesList = transactionsRepository.findByCustomerIdIn(customerEntitiesList);

を実行しようとするということですこの例外が発生する:

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value element [[email protected]] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [[email protected]] 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; 
    } 
} 
+0

ような何かを行うことができますか? – dudel

+0

元の投稿を更新しました。一方、私は問題を解決し、オブジェクトをクエリに渡していましたが、Stringが必要でした。私は春のデータをあまりにも多くのクレジットを与えていたと思う:)私は最初に顧客のリストを取得するので、私は結果に満足していない、私は別のクライアントID(文字列)値のリストを作成し、 。多分別の方法がありますか? –

答えて

1

それは例外春に言うように、あなたのTransactionEntityであなたのcustomer_idはStringですが、あなたはCustomerEntityを入力しているためStringを期待しています。代わりにList<String>にお客様IDのリストを入力する必要があります。ところで

はあなたのcustomer_idはあなたのCustomerEntityidに設定すると仮定しintすべきではありませんか?あなたもあなたのTransationEntityを投稿することができます

次に、あなたは

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

ありがとう!これは動作します! –

関連する問題