Nikzin
Nikzin

Reputation: 366

Spring JPA @OneToMany Parameter value [] did not match expected type

I get this error when requesting service line:

List<Order> orders = ordersRepository.getByCustomerId(id);

Error: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [5] did not match expected type [nz.webshop.models.Customer.Customers (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [5] did not match expected type [nz.webshop.models.Customer.Customers (n/a)]

Repository:

public interface OrdersRepository extends JpaRepository<Order, Integer> {
    List <Order> getByCustomerId(Integer customerId);
}

Entities:

@EntityA:
@Table(name = "orders")
public class Order {
@ManyToOne(targetEntity=Customers.class)
   @JoinColumn (name = "customer_id", referencedColumnName="customer_id")
    private Integer customerId;

...getters/setters




@EntityB:
@Table (name ="customer")
public class Customers {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "customer_id")
    private Integer customerId;

    @OneToMany(targetEntity = Order.class, mappedBy = "customerId")
    private List<Order> Order;

...getters/setters

Where tables are like this:

CREATE TABLE orders
(
  order_id           INT AUTO_INCREMENT PRIMARY KEY,
  customer_id INT,
 FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);


CREATE TABLE customer
(
  customer_id INT AUTO_INCREMENT PRIMARY KEY,
  first_name  VARCHAR(50) ,
  last_name   VARCHAR(50) 

    );

Update: I changed EntityA as follows:

@EntityA:
@Table(name = "orders")
public class Order {

@Column(name = "customer_id") 
private Integer customerId;

@ManyToOne(targetEntity=Customers.class)
    private Customer customer;

...getters/setters

But then when requesting the same service line new error: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'order0_.customer_customer_id' in 'field list'.

Where is the problem could be?

Upvotes: 2

Views: 2026

Answers (1)

Gabriel Pizarro
Gabriel Pizarro

Reputation: 470

It looks like the Order.customerId property is not set up correctly. You're requesting that ManyToOne find Customer models based on their IDs, but then you're loading the models as Integers. Try this instead:

@ManyToOne
@JoinColumn(name = "customer_id", referencedColumnName="customer_id")
private Customer customer;

Let me know if this works, and if not, we can troubleshoot some more.

Upvotes: 2

Related Questions