这里有两个实体,一个是客户,另一个是订单,一个客户可以获取多个订单,最后我使用springmvc在控制器中返回了一个客户对象,但是它总是给我sqlgrammer错误,杰克逊·宾德能做到吗?多对多的问题?
这是控制器中的代码片段:
@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;
}
和错误跟踪
org.springframework.web.util.NestedServletException:请求
处理失败;嵌套异常为
org.hibernate.exception.SQLGrammarException:无法执行
声明
客户实体
@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...
这是命令:
@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...
**更新:**也许是tomcat缓存问题,我记得当时已经重新启动了服务器,没有做任何更改,但是在关闭项目几个小时并重新打开它之后,确定!
最佳答案
对于此错误,建议的方法是添加spring.jpa.show-sql=true
以打印sql语句,然后可以检查sql。典型地,它包含关键字,例如“订购”