Hibernate:不成功的alter table

时间:2013-03-11 11:42:09

标签: mysql hibernate nhibernate-mapping

我正在使用Hibernate 4,我想快速测试@OrderColumn注释但是我在尝试这样做时遇到以下错误:

INFO [main] (SchemaExport.java:343) - HHH000227: Running hbm2ddl schema export
DEBUG [main] (SchemaExport.java:353) - Import file not found: /import.sql
DEBUG [main] (SqlStatementLogger.java:104) - 
    alter table Order 
        drop 
        foreign key FK48E972E19D16493
Hibernate: 
    alter table Order 
        drop 
        foreign key FK48E972E19D16493
ERROR [main] (SchemaExport.java:425) - HHH000389: Unsuccessful: alter table Order drop foreign key FK48E972E19D16493
ERROR [main] (SchemaExport.java:426) - 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 
        drop 
        foreign key FK48E972E19D16493' at line 1
DEBUG [main] (SqlStatementLogger.java:104) - 
    drop table if exists Customer
Hibernate: 
    drop table if exists Customer
DEBUG [main] (SqlStatementLogger.java:104) - 
    drop table if exists Order
Hibernate: 
    drop table if exists Order
ERROR [main] (SchemaExport.java:425) - HHH000389: Unsuccessful: drop table if exists Order
ERROR [main] (SchemaExport.java:426) - 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' at line 1
DEBUG [main] (SqlStatementLogger.java:104) - 
    create table Customer (
        id integer not null auto_increment,
        primary key (id)
    )
Hibernate: 
    create table Customer (
        id integer not null auto_increment,
        primary key (id)
    )
DEBUG [main] (SqlStatementLogger.java:104) - 
    create table Order (
        id integer not null auto_increment,
        number varchar(255),
        customer_id integer,
        orders_index integer,
        primary key (id)
    )
Hibernate: 
    create table Order (
        id integer not null auto_increment,
        number varchar(255),
        customer_id integer,
        orders_index integer,
        primary key (id)
    )
ERROR [main] (SchemaExport.java:425) - HHH000389: Unsuccessful: create table Order (id integer not null auto_increment, number varchar(255), customer_id integer, orders_index integer, primary key (id))
ERROR [main] (SchemaExport.java:426) - 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 (
        id integer not null auto_increment,
        number varchar(255),' at line 1
DEBUG [main] (SqlStatementLogger.java:104) - 
    alter table Order 
        add index FK48E972E19D16493 (customer_id), 
        add constraint FK48E972E19D16493 
        foreign key (customer_id) 
        references Customer (id)
Hibernate: 
    alter table Order 
        add index FK48E972E19D16493 (customer_id), 
        add constraint FK48E972E19D16493 
        foreign key (customer_id) 
        references Customer (id)
ERROR [main] (SchemaExport.java:425) - HHH000389: Unsuccessful: alter table Order add index FK48E972E19D16493 (customer_id), add constraint FK48E972E19D16493 foreign key (customer_id) references Customer (id)
ERROR [main] (SchemaExport.java:426) - 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 
        add index FK48E972E19D16493 (customer_id), 
        add constrain' at line 1
 INFO [main] (SchemaExport.java:405) - HHH000230: Schema export complete

以下是使用的代码:

Customer.java

@Entity
public class Customer {
       private Integer id;
       private List<Order> orders;

       @Id @GeneratedValue 
       public Integer getId() { return id; }
       public void setId(Integer id) { this.id = id; }

       @OneToMany(mappedBy="customer")
       @OrderColumn(name="orders_index")
       public List<Order> getOrders() { return orders; }
       public void setOrders(List<Order> orders) { this.orders = orders; }

       public void addOrder(Order order) {
           this.orders.add(order);
           order.setCustomer(this);
       }
}

Order.java

@Entity
public class Order {
       private Integer id;
       private String number;
       private Customer customer;

       public Order() {}   
       public Order(String number) { this.number = number; }


    @Id @GeneratedValue 
       public Integer getId() { return id; }
       public void setId(Integer id) { this.id = id; }

       public String getNumber() { return number; }
       public void setNumber(String number) { this.number = number; }

       @ManyToOne
       @JoinColumn(name="customer_id")
       public Customer getCustomer() { return customer; }
       public void setCustomer(Customer customer) { this.customer = customer; }
}

TestClient.java

public class Client {
    public static void main(String[] args) {        
        SessionFactory sessionFactory =  new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction txn = session.beginTransaction();

        //inserting data into tables

        Customer leo = new Customer();
        List<Order> orders = new ArrayList<Order>();
        Order book = new Order("1-sia-2nd-edi");
        Order mobile = new Order("2-son-wal");
        Order ipod = new Order("3-ipo-app");
        orders.add(book);orders.add(mobile);orders.add(ipod);
        leo.setOrders(orders);

        session.save(leo);


        //retrieving data from tables

        txn.commit();
        session.close();            
    }
}

hibernate.cfg.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">pass</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

  <property name="hibernate.hbm2ddl.auto">create</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.use_sql_comments">true</property>

  <mapping class="com.transbinary.entity.Customer"/>
  <mapping class="com.transbinary.entity.Order"/>  
 </session-factory>
</hibernate-configuration>

有人可以帮我理解为什么我会收到这个错误吗?

1 个答案:

答案 0 :(得分:6)

order是mysql中的关键字。所以使用不同的名称。我认为这可能是问题所在。 如果您更改表名,它将起作用。我测试了它。