Reputation: 684
Here are two entities, one is Customer and the other is Order, and one customer can obtain multiple Order, finally i return a Customer object in the controller using springmvc, but it always gave me sqlgrammer error, does jackson-bind can deal one-to-many problem? here is the code snippet in the controller:
@RequestMapping(value = "/getCustomer", method = RequestMethod.GET)
public @ResponseBody Customer getCustomer(){
Customer customer = new Customer();
customer.setRecord("Crabime");
customer.setGender("male");
Order order = new Order("vegitable", 12.1);
Order order1 = new Order("fruit", 3.2);
Set<Order> set = new HashSet<Order>();
set.add(order);
set.add(order1);
customer.setOrders(set);
customerService.save(customer);
return customer;
}
and error trace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
Customer Entity
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String record;
private String gender;
@OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "customer")
private Set<Order> orders;
public Customer(){}
...omit setter and getter...
here is the Order :
@Entity
@Table(name = "order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private double price;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "customer_id")
private Customer customer;
public Order() {
}
public Order(String name, double price) {
this.name = name;
this.price = price;
}
...omit setter and getter...
**UPDATE:**maybe it's tomcat cache problem, i remember having restarted server at that time and without anything changed, but after closing the project for few hours and reopen it, ok!
Upvotes: 1
Views: 1355
Reputation: 4476
For this error, the suggest way is add spring.jpa.show-sql=true
to print the sql statement, then you can check the sql. Typical it contains the keywords, e.g. "order"
Upvotes: 2