sql - SQL下面的GORM(grails)条件是什么?

标签 sql hibernate grails gorm

SQL下的GORM准则将是什么

订单表:

  • OrderID
  • 客户ID
  • 订购日期

  • 客户表:
  • 客户ID
  • 客户名称
  • 联系人姓名
  • 国家

  • Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column. Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables:strong text


    SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
    FROM Orders
    INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
    

    Sql将使用那些列产生那些数据
  • OrderID
  • 客户名称
  • 订购日期

  • GORM模型:
    class Order{
    
    Long orderId
    Long customerId
    def orderDate
    }
    
    class Customer{
    
    Long customerID
    Long customerName
    def contactName
    def country
    }
    
    
    
    

    最佳答案

    我正在做出一些猜测,以创建一个与您在那里描述的持久性模型相匹配的对象模型。

    class Customer {
        String customerName
        String contactName
        String country
    
        static mapping = {
            table 'Customers'
            version false
            id column: 'CustomerID'
            customerName column: 'CustomerName'
            contactName column: 'ContactName'
            country column: 'Country'
        }
    }
    
    class Order {
        Customer customer
        Date orderDate
    
        static mapping = {
            table 'Orders'
            version false
            id column: 'OrderID'
            customer column: 'CustomerID'
            orderDate column: 'OrderDate'
    
        }
    }
    

    执行如下查询:
    Order.findAll {
        createAlias 'customer', 'cust'
        projections {
            property 'id'
            property 'orderDate'
            property 'cust.customerName'
        }
    }
    

    这将导致如下所示的SQL:
    select this_.OrderID as y0_, this_.OrderDate as y1_, cust1_.CustomerName as y2_ from Orders this_ inner join Customers cust1_ on this_.CustomerID=cust1_.CustomerID
    

    关于sql - SQL下面的GORM(grails)条件是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114242/

    相关文章:

    grails - Atmosphere :如何从 Grails 中的 webSocket 连接获取登录用户?

    java - 阻止 Hibernate 更新现有记录

    hibernate - 从 glassfish 4 迁移到 wildfly 8.1 后,AttributeConverter 失败

    java - 无 Spring Hibernate DAO 和 @Repository 用于异常翻译!这不是依赖吗?

    Java Hibernate MySQL 表锁定

    grails - 具有ldap授权的Grails Spring Security ui插件?

    oracle - Grails 与 Oracle-DB : seems to have only one global counter for IDs

    sql - 排序后只获取最后一行

    php - 通过php发送html表单数据到sql数据库(使用mysqli)

    sql - PL/SQL 对具有相同 ID 的行进行编号