3 个实体之间的域关联
Domain association among 3 entities
我正在开发 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', ''
}
您将 table 命名为 order
,这是保留的 SQL 关键字。选择另一个 table 名称。
我正在开发 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', ''
}
您将 table 命名为 order
,这是保留的 SQL 关键字。选择另一个 table 名称。