3个实体之间的域关联

时间:2015-04-19 12:01:06

标签: hibernate grails gorm

我正在开发Grails应用程序。我创建了3个客户,产品和订单实体。客户可以拥有多个订单,但每个订单只能包含一个产品。因此,单个订单可以与1个客户和1个产品相关联。这是我的域名:

客户:

class Customer {
    String name;
    String address;
    Integer phoneNumber;
    Date dateCreated

    static hasMany = [orders:Order]
    static constraints = {
        name nullable: false
        address nullable: false
    }
}

订单:

class Order {
    Customer customer
    Product product
    Date orderDate

    static hasOne = [customer:Customer,product:Product]

    static constraints = {

    }
}

产品:

class Product {
    String name;
    String description;
    Date dateCreated

    static hasMany = [orders:Order]

    static constraints = {
    }
}

我使用generate-all生成控制器,视图,当我运行应用程序时,我收到此错误:

| Running Grails application
| Error 2015-04-19 17:22:35,289 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - Unsuccessful: create table order (id bigint not null auto_increment, version bigint not null, customer_id bigint not null, order_date datetime not null, product_id bigint not null, primary key (id)) ENGINE=InnoDB
| Error 2015-04-19 17:22:35,290 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - 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 bigint not null auto_increment, version bigint not null, customer_id b' at line 1
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - Unsuccessful: alter table order add index FK651874E2B2D0780 (product_id), add constraint FK651874E2B2D0780 foreign key (product_id) references product (id)
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - 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 FK651874E2B2D0780 (product_id), add constraint FK651874E2B2D0780' at line 1
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - Unsuccessful: alter table order add index FK651874E89AD9194 (customer_id), add constraint FK651874E89AD9194 foreign key (customer_id) references customer (id)
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - 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 FK651874E89AD9194 (customer_id), add constraint FK651874E89AD919' at line 1

此外,我无法创建客户。域映射有什么问题吗?

我的数据源配置如下:

dataSource {
    driverClassName="com.mysql.jdbc.Driver"
    dialect="org.hibernate.dialect.MySQL5InnoDBDialect"
    url="jdbc:mysql://localhost:3306/aprilapp?useUnicode=true&characterEncoding=UTF-8"
    username="root"
    password="root"
    dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
}

1 个答案:

答案 0 :(得分:1)

您将表命名为order,这是一个保留的SQL关键字。选择另一个表名。