当前位置: 首页 > 知识库问答 >
问题:

3个实体之间的域关联

郭意
2023-03-14

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

顾客:

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个答案

孙夕
2023-03-14

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

 类似资料:
  • 我试图在JPA中规划这种关系,但似乎在这里迷失了方向。这是我的ER型号说明。我有一个,其中一个客户有一个,这个包含股票。这就是我的想法。每个都有一个,即(关系)1:1,仓库可以包含更多的股份(库存)。仓库-- 我有以下代码。 Customer.java Depot.java 使用上面的代码,我甚至可以创建并持久化客户。我是不是做错了地图? 如果我试图将数据持久化到上面的数据库中,我会收到以下错误消

  • 问题内容: 我想要3个Java类的对象之间的 单向 一对一关系:“人到心脏”和“人到肝脏”。我希望对象共享相同的PK,即每个人都有相应的心脏和肝脏,其中person.person_id = heart.heart_id = liver.liver_id。我不想将3个表合并为1个表,因为每个表都有很多字段。这是我的代码(主要基于该问题的公认答案): 我设置了会话并执行以下操作: 然后,如果我将人对象

  • 我很难找到数据库中多对多关系的最佳设计。我的项目允许用户创建我们所说的日志警报。日志警报将检查给定的日志是否满足某些标准,如果满足,它将向AWS SNS主题发送消息。我想做的是将日志警报与AWS SNS主题联系起来。我还想将哪个用户分配日志警报与AWS SNS主题联系起来。

  • 假设我们的环境中有三个实体:教师、学生和课程。 每位教师都有(教授)1门或1门以上的课程,每门课程由0名或0名以上的教师提供 每个学生都选修了1门或更多课程,并且每门课程都有0名或更多学生选修 每个教师有0个或多个学生,每个学生有1个或多个教师 在这种关系中,每一种关系都可以从另外两种关系中推断出来,例如,要知道T1老师正在教哪些学生,要通过教师和课程之间的关系来了解T1老师正在教哪些课程,然后通

  • 假设我有2个实体类,User和post。 波纹管示例

  • 我很难模拟这种情况。我在设计一个基于位置的优惠券系统。用户可以定义区域和优惠,然后将每个优惠关联到多个区域。此外,每个区域可以有多个优惠。所以zone和offer有很多对很多的关系。用户实体拥有这两个实体。只有在同一用户拥有区域和优惠的情况下,才能将它们关联起来。图表会很有帮助。多谢了。