Hibernate SQLGrammarException:用于具有3种不同关联映射的表

时间:2014-09-16 09:12:35

标签: java mysql hibernate

我正在尝试编写简单的项目,但我遇到了无法解决的问题。我搜索了解决方案,但没有什么真正起作用。所以问题是:

我的猜测是表创建的问题。表顺序与第一个表(客户表)具有多对一关系,与第二个表(订单项)具有一对多关系,与第三个表(addressess表)具有一对一的双向关系。我一遍又一遍地检查语法,但对我来说似乎没问题。应该是我错过了一些东西。请帮忙!!

下面列出了代码,并附有错误日志。

@Entity
@Table(name="order")
public class Order {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="orderID")
private int orderID;

@ManyToOne
@JoinColumn(name="cid",referencedColumnName="cid")
private Customer customer;

@OneToOne
@JoinColumn(name="addid")
private ShippingAddress address;

@OneToMany(mappedBy="order")
private Set<OrderItem> orderItems;

..... some for fields, constructors, setters and getters.

类 - &gt; OrderItem的

@Entity
@Table(name="orderitems")
public class OrderItem {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="otid")
private int otid;


@ManyToOne
@JoinColumn(name="orderID",referencedColumnName="orderID")
private Order order;

@OneToOne
@JoinColumn(name="bid")
private Book book;

... More fields and constructors , setters and getters..

班级送货地址

@Entity
@Table(name="addressess")
public class ShippingAddress {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="addid")
private int addid;

@OneToOne
@JoinColumn(name="addid")
private Order order;

... More fields and constructors , setters and getters..

课本

@Entity
@Table(name="books")
public class Book {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="bid")
private int bid;

@ManyToMany
@JoinTable(name="books_author",
joinColumns=
    @JoinColumn(name="bid",referencedColumnName="bid"),
inverseJoinColumns=
    @JoinColumn(name="aid",referencedColumnName="aid"))
private Set<Author> author;


... More fields and constructors , setters and getters..

导致错误的代码......

package com.jlcindia.hibernate;

public class Client3 {
public static void main(String[] args) {
try{
    SessionFactory sf=AHibernateUtil.getSessionFactory();
    Session session=sf.openSession();
    Transaction tx=session.beginTransaction();

    Customer cust=(Customer) session.load(Customer.class,1);
    Book b1=(Book) session.load(Book.class,1);
    OrderItem oi1= new OrderItem(1,1000);
    oi1.setBook(b1);        
    session.save(oi1);

    Book b2=(Book) session.load(Book.class,2);
    OrderItem oi2= new OrderItem(2,2000);
    oi2.setBook(b1);
    session.save(oi2);

    ShippingAddress add= new ShippingAddress("AMP","HYD","AP");
    session.save(add);
    Set<OrderItem> orderItems= new HashSet<OrderItem>();
    orderItems.add(oi1);
    orderItems.add(oi2);

    Order order= new Order(3,3000.0, new Date(),"New");
    order.setAddress(add);
    order.setOrderItems(orderItems);
    order.setCustomer(cust);
    session.save(order);


    tx.commit();
    session.close();

}catch(Exception e){
    e.printStackTrace();
}
}
}

创建的表将具有多对一的customer表映射,一个映射为addressess表,一个映射到orderItems。

我得到的例外是:

org.hibernate.exception.SQLGrammarException: could not insert: [com.jlcindia.hibernate.Order]
...............
................
Caused by: java.sql.SQLException: Syntax error or access violation,  message from server: "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 (addid, cid, orderDate, status, totalCost, totalqty) values (2, 1, '2014-0' at line 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
... 16 more

我无法得到这个。事实上,我甚至不知道我哪里出错了。

1 个答案:

答案 0 :(得分:2)

多年前我也遇到过同样的问题......它太傻了......

你不能将order作为名称放在带有注释的Hibernate + MySQL中的表中,因为order是MySQL中的保留字。

您可以通过示例检查这是否容易将order更改为order_

希望这有帮助!