2016-12-05 44 views
1

我有这样一个域名...获取父数据数组列表

class Invoice { 

    String invoiceNo 
    Date invoiceDate 
    Date dueDate 
    BigDecimal totalAmount 

    Date sendDate 
    Customer customer 
    String fromSOorDO   

    static mapping = { 
     //formula to calculate dueDate (invoiceDate + tempo) 
     dueDate formula:"ADDDATE(invoice_date, tempo)" 
    } 

    static constraints = {   
    } 

} 

我需要从域invoice所有数据传递到Array Listparent的名称,而不是id

例如,从域invoice,我们可以看到Customer customer

当我们从数据库中看到,现场customerid123其中属于表Customer

这是客户的域名。

class Customer { 
    String code 
    String name 
    String address 
    String phoneNo 

    static constraints = {} 
} 

我需要将所有数据通过发票成ArrayList其中Customerid替换Customername ..

实施例的数据:

invoice具有customer = 1

然后表客户这样:

id | code | name | address| phoneNo| 
1 | A |aaaaaa| Street |123132 | 
2 | B |bbbbbb| Street |454545 | 

当我检索域invoicecustomer的字段显示为客户的名称并将其填充到哈希映射。

def invoice = c.list(params) { 
    ge("invoiceDate", dateFrom) 
    eq("deleteFlag", "N") 
    eq("cif", cif) 
} 

def map = new HashMap() 
map.put("invoiceList",invoice) 

我试图用collect喜欢这一点,并得到这个错误:

def c = Invoice.createCriteria() 
    def invoice = c.list(params) { 
     ge("invoiceDate", dateFrom) 
     eq("deleteFlag", "N") 
     eq("cif", cif) 
    }.collect{ invoice -> 
     invoice.properties.collect{ it } + [customer:invoice.customer.name] 
    } 



ERROR org.grails.web.errors.GrailsExceptionResolver - IllegalStateException occurred when processing request: [POST] /api/listofinvoice/index - parameters: 
lastupdate: 01/01/2016 
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here. Stacktrace follows: 
java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here 
     at grails.beans.util.LazyMetaPropertyMap.entrySet(LazyMetaPropertyMap.java:224) ~[grails-core-3.1.1.jar:3.1.1] 
     at com.vastpalaso.market.InvoiceController$_closure14$_closure23$$EQ4ZWTHZ.doCall(InvoiceController.groovy:607) ~[na:na] 
     at com.vastpalaso.market.InvoiceController$_closure14$$EQ4ZWTHV.doCall(InvoiceController.groovy:602) ~[na:na] 
     at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64) ~[spring-security-core-3.1.1.jar:na] 
     at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) ~[spring-security-core-3.1.1.jar:na] 
     at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) ~[spring-security-core-3.1.1.jar:na] 
     at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58) ~[spring-security-core-3.1.1.jar:na] 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_111] 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_111] 
     at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] 
+0

你真的认为我们需要查看您的域类的所有属性?包括那些,评论一些巨型巨无霸? – injecteer

+0

@injecteer完成 – akiong

回答

1

你有2个选择这里:

1)收集客户的名称代码:

def invoice = c.list(params) { 
    ge("invoiceDate", dateFrom) 
    eq("deleteFlag", "N") 
    eq("cif", cif) 
}.collect{ invoice -> invoice.properties.collect{ it } + [ customer:invoice.customer.name ] } 

2)使用projection

def invoice = c.list(params) { 
    projections { 
     property 'invoiceNo' 
     property 'invoiceDate' 
     // other properties 
     customer { 
      property 'name' 
     } 
    } 
    ge("invoiceDate", dateFrom) 
    eq("deleteFlag", "N") 
    eq("cif", cif) 
} 

注意,在这两种情况下,你得到Map小号的实例,而不是你的Invoce对象列表

+0

是的,这一个...我喜欢它,谢谢.. – akiong

+0

如果我想通过更多,不仅客户,然后这样? '.collect {invoice - > invoice.properties.collect {it} + [customer:invoice.customer.name,customer2:invoice.customer2。}' – akiong

+0

我试着用'collect'使用你的解决方案,看到我更新的问题 – akiong

1

尝试这样的..

def c = Invoice.createCriteria() 
    def invoice = c.list(params) { 
     createAlias('customer','customer') 
     projections { 
      property ('invoiceNo','invoiceNo') 
      property ('invoiceDate','invoiceDate') 
      property ('dueDate','dueDate') 
      property ('totalAmount','totalAmount') 
      property ('sendDate','sendDate') 
      property ('customer.name','customer.name') 

     } 

     ge("invoiceDate", dateFrom) 
     eq("deleteFlag", "N") 
     eq("cif", ciff) 
    } 

看到这一点:Question

+0

你是我的英雄:D – akiong