Search code examples
grailsgrails-ormgrails-domain-classgrails3

embedded grails sql views


I have a question about using sql views and "embedded" fields in domain classes. I created a sql view (which contains, among other things, name, shipping_name, billing_name columns) that I associated with a modeled domain class as below. Both classes are in the same file. I tried to move WebOrdressAddress in it's own file (in src/main/groovy) but it does not work.

class WebOrderView {

    String name

    WebOrderAddress shipping

    WebOrderAddress billing

    static embedded = ["shipping", "billing"]

    static mapping = {
        table 'web_order_vw'
        version false
    }
}

class WebOrderAddress {
    String name
}

When I use a finder (like WebOrderView.findByName('test')) I get "null". If I comment the lines "embedded", "shipping", "billing" and I use the same finder I get a result. Can you tell me what's wrong with embedded fields ? Do embedded fields work with sql views? I'm using Grails 3.2.11

Thanks


Solution

  • Finally i found a solution after i seen a strange behavior in gorm. I renamed the "name" field in the sql view (and in WebOrderView domain class).

    class WebOrderView {
    
        String pname
    
        WebOrderAddress shipping
    
        WebOrderAddress billing
    
        static embedded = ["shipping", "billing"]
    
        static mapping = {
            table 'web_order_vw'
            version false
        }
    }
    
    class WebOrderAddress {
        String name
    }
    

    With SQL enabled, without embedded fields i got the following sql

    select
        this_.name as name1_18_0_,
    from
        web_order_vw this_ 
    where
        this_.name=? limit ?
    

    If i use embedded fields, the field WebOrderView.name bas been replaced by billing_name (WHY GORM ?!) and the generated criteria is not the same (even if i use the same finder)

    select
        this_.billing_name as billing18_18_0_,
        this_.shipping_name as shippin25_18_0_
    from
        web_order_vw this_ 
    where
        this_.billing_name=? limit ?