这里有两个实体,一个是Customer,另一个是Order,一个customer可以获取多个Order,最后我使用springmvc在 Controller 中返回一个Customer对象,但是它总是给我sqlgrammer错误,jackson-bind可以吗处理一对多问题? 这是 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;
}
和错误跟踪
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
客户实体
@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了。通常它包含关键字,例如“订单”
关于java - org.springframework.web.util.NestedServletException : nested exception is org. hibernate.exception.SQLGrammarException:无法执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484732/