Grails query - can't access join table

妖精的绣舞 提交于 2020-01-07 03:01:15

问题


I want to make a list of all the SAMPLE_PARAMETERS that belong to a SAMPLE_TYPE. I have two classes: A Sample_type and a Sample_parameter. There is also a join table which grails automagically made that lists all the ids of the type/parameters called Sample_Type_Sample_Parameters. I am trying to make a list of all the parameters that belong to a certain type, but cannot figure out how to access the join table.

Can I use withCriteria? if so, how would one go about doing so? In pseudo-code: For instance, if my sample type is blood - I want this

def result = SampleParameters.withCriteria{
//all parameters in which the sampleType.id in join table matches the blood's id    
}

My Classes:

class SampleType {

    String sampleName

    static constraints = {
         sampleName(blank:false)
    }
    String toString() {
        "${sampleName}"
    }

    static hasMany =[sampleParameters:SampleParameter]//[tags:Tag]
    static mappedBy=[sampleParameters:"sampleTypes"]//[tags:"domainClass2s"]
}

class SampleParameter {

    String name
    String value

    static hasMany = [
    samples:Sample,         //domainClass1s: DomainClass1,
    sampleTypes:SampleType  //domainClass2s: DomainClass2
    ]

    static mappedBy = [samples:"sampleParameters",sampleTypes:"sampleParameters"]//[domainClass1s: "tags", domainClass2s: "tags"]
    static belongsTo =[Sample,SampleType] //[DomainClass1, DomainClass2]

    static constraints = {
      name()
      value(unique:true)
    }

    @Override public String toString() {
        return value
    }
}

回答1:


You should just be able to do:

def props = SampleType.findBySampleName( "Blood" ).sampleProperties

but it depends on how you have defined your domain classes, which you have omitted from your question



来源:https://stackoverflow.com/questions/10643491/grails-query-cant-access-join-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!