I am trying to implement order management system by hibernate, which I am not very familiar with. The exception asked me to manually execute sql the same as hibernate in my program tried, and it succeeded. I don't know what's the problem. Probably it's from design issues. Database is like this:
ORDER has a foreign key ITEM_ID pointing to ITEM_ID in ITEM table, it's a one to many relationship and another foreign key CLIENT_ID pointing to CLIENT_ID in CLINET table. Three entities' definition with annotation are written as
@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
}
The order data insertion function is like:
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();
}
Here is my 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
I have tried that sql manually, and it did insert successfully. I can't find any clue where the problem could be. Please help.