gpt4 book ai didi

java - "ERROR: You have an error in your SQL syntax;"in hibernate 一对多关系映射

转载 作者:可可西里 更新时间:2023-11-01 07:59:01 24 4
gpt4 key购买 nike

我正在尝试通过hibernate实现订单管理系统,我不是很熟悉。异常要求我手动执行sql 和我程序中的hibernate 一样试过了,成功了。我不知道出了什么问题。应该是设计问题吧。数据库是这样的:enter image description here

ORDER 有一个外键 ITEM_ID 指向 ITEM 表中的 ITEM_ID,它是一对多关系,另一个外键 CLIENT_ID 指向 CLINET 表中的 CLIENT_ID。三个带注解的实体定义写成

@Table(name = "ITEM")
public class Item implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ITEM_ID", unique = true, nullable = false)
private int item_id;
@Column(name = "NAME")
private String name;
@Column(name="CATEGORY")
private String category;
@Column(name="UNIT")
private String unit;
@Column(name="PRICE")
private double price;
//getters and setters
}

@Entity
@Table(name="CLIENT")
public class Client implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CLIENT_ID", unique = true, nullable = false)
private int client_id;
@Column(name="CLIENT_NAME")
private String client_name;
//getters and setters
}

@Entity
@Table(name="ORDER")
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ORDER_ID", unique = true, nullable = false)
private int order_id;
@ManyToOne(cascade = CascadeType.PERSIST,targetEntity=Client.class)
@JoinColumn(name = "CLIENT_ID", nullable = false)
private Client client;
@ManyToOne(cascade = CascadeType.PERSIST,targetEntity=Item.class)
@JoinColumn(name = "ITEM_ID", nullable = false)
private Item item;
@Column(name="QUANTITY")
private double quantity;
@Temporal(TemporalType.DATE)
@Column(name = "ORDER_DATE")
private Date order_date;
//getters and setters
}

订单数据插入函数如下:

public static void insertNewOrder(Item item,Client client, double quantity, Date date){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Order order = new Order();
order.setItem(item);
order.setClient(client);
order.setQuantity(new Double(quantity));
order.setOrder_date(date);
session.save(order);
session.getTransaction().commit();
session.close();
}

这是我的异常(exception)情况:

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER (CLIENT_ID, ITEM_ID, ORDER_DATE, QUANTITY) values (1, 1, '2013-11-12', 1.4' at line 1
Exception in thread "main" org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER (CLIENT_ID, ITEM_ID, ORDER_DATE, QUANTITY) values (1, 1, '2013-11-12', 1.4' at line 1

我已经手动尝试了那个sql,它确实插入成功了。我找不到问题可能出在哪里的任何线索。请帮忙。

最佳答案

ORDER 是一个 MySQL 关键字。如果您必须将它用作表名,则需要将每个引用都用反引号括起来。

SELECT * FROM `ORDER`...

我建议您将表名更改为不冲突的名称

关于java - "ERROR: You have an error in your SQL syntax;"in hibernate 一对多关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com