我正在为练习项目在线购物系统开发简单的API。由于我是使用API的新手,因此我的实体和关系出现问题。首先,在介绍问题之前,我给出了所有的模式和类。
这是我的数据库架构的link。
这些是@Entity
类:
----
@Entity
@Table(name = "Customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "country")
private String country;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Order> orders = new ArrayList<>();
// constructor, getters, setters ....
@Entity
@Table(name = "Order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "date")
private Date date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cust_id", nullable = false)
@JsonIgnore
private Customer customer;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Detail> details = new ArrayList<>();
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Invoice> invoices = new ArrayList<>();
//constructor, setters, getters ....
@Entity
@Table(name = "Product")
public class Product {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "price")
private Double price;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Detail> orderDetails = new ArrayList<>();
//cons, setters, getters ...
@Entity
@Table(name = "Detail")
public class Detail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ord_id", nullable = false)
@JsonIgnore
private Order order;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pr_id", nullable = false)
@JsonIgnore
private Product product;
@Column(name = "quantity")
private int quantity;
//similar classes for Invoice and Payment (no problem with them)
这是我的Sample Repository类:
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
}
这是我的控制器类:
@RestController
public class OnlineShoppingApiController {
@Autowired
ProductRepository productRepository;
@Autowired
OrderRepository orderRepository;
@Autowired
CustomerRepository customerRepository;
@Autowired
DetailRepository detailRepository;
@Autowired
InvoiceRepository invoiceRepository;
@Autowired
PaymentRepository paymentRepository;
@GetMapping("/products")
public List<Product> getProductsList(){
return productRepository.findAll();
}
@GetMapping("/customers")
public List<Customer> getCustomersList(){
return customerRepository.findAll();
}
@GetMapping("/orders")
public List<Order> getOrdersList(){
return orderRepository.findAll();
}
@GetMapping("/invoices")
public List<Invoice> getInvoicesList(){
return invoiceRepository.findAll();
}
@GetMapping("/payments")
public List<Payment> getPaymentsList(){
return paymentRepository.findAll();
}
@GetMapping("/details")
public List<Detail> getDetailsList(){
return detailRepository.findAll();
}
我对所有API和关系采用相同的方法。
当我在邮递员中呼叫/产品时,我得到的结果是JSON:
[{
"id": 3,
"name": "pname_816",
"description": "pdesc_871_871_871_87",
"price": 1.41,
"orderDetails": [
{
"id": 9,
"quantity": 831
},
{
"id": 51,
"quantity": 701
},
{
"id": 87,
"quantity": 310
}
]
},
{
"id": 4,
"name": "pname_395",
"description": "pdesc_495_495_495_49",
"price": 26.65,
"orderDetails": [
{
"id": 85,
"quantity": 853
}
]
}]
/details
,/invoices
和/payments
的结果相同。
问题是如果我发送/customers
的GET请求,结果是:
{
"timestamp": "2018-04-05T11:53:39.558+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.example.project.pojo.Customer[\"orders\"])",
"path": "/customers"
}
如果我发送/orders
请求,结果是:
{
"timestamp": "2018-04-05T11:54:37.316+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet",
"path": "/orders"
}
同样的方法并非适用于所有人。我无法理解问题出在哪里。请帮我找到它。
感谢您的回答
答案 0 :(得分:0)
我终于找到了自己的答案。这里的问题不在于注释或键引用,而在于命名实体。
由于order是MySql的保留关键字,因此命名实体和这样的变量会导致意外问题。
所以我刚刚在架构和代码中将实体名称更改为Orders并且工作正常。
希望这篇文章对其他人也有帮助