且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Grails查询 - 无法访问连接表

更新时间:2023-02-06 20:03:42

只需要能够做到:

$ $ $ $ $ $ $ $ $ $ $> def props = SampleType.findBySampleName(Blood).sampleProperties

,但这取决于您如何定义您的域类,而您从问题中省略了它。


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
    }
}

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