1

我有两个类:样本和参数。我还有一个 sample_sample_parameter 查找表,用于保存样本 id 和参数 id。这是在我的 grails 应用程序中映射的。

我能够编写一个适用于松鼠的 sql 查询:

select s.* from sample s, sample_sample_parameters sp where s.id = sp.sample_id and sp.sample_parameter_id = 41

其中 41 将替换为从 gsp 页面传递给操作的 parameter.id 变量。我也试图让它与 executeQuery 一起工作,但它告诉我样本没有被映射。

如何将此查询转换为可识别的形式?

class Sample {


Date collectionDate // date the sample was collected
Date sampleReceivedDate // date the sample arrived on site
Date dateCreated
String sampleComments // details about the sample
String labAccessionID // internal reference
String sampleGender // is it from a male or female?
String sampleAge // the age of the animal the sample was taken from
String sampleBreed // the breed of animal
String sampleNameID // patient name
String filepath

String enteredby


String sg
String mosm

static searchable = true


static hasMany =[sampleParameters:SampleParameter, ficsruns:Ficsrun]//[tags:Tag]// a Sample can have many parameters
/* mappedBy allows two tables share a common table. It creates two join tables, one for each. 
 * SampleParameter is the table being shared by Sample and SampleType tables */
static mappedBy=[sampleParameters:"samples"]//[tags:"domainClass1s"]/*http://www.van-porten.de/2010/09/multiple-many-to-many-in-grails/*/
static belongsTo = [sampleType:SampleType, labFinding:LabFinding, sampleSource:SampleSource, species:SpeciesList] // creates dependencies


static constraints = {

    sampleType(blank:false)
    sampleNameID(blank:false)
    collectionDate(blank:false)
    sampleReceivedDate(blank:false)
    sampleComments(nullable:true, maxSize:1000)
    labAccessionID(nullable:true)
    sampleGender(blank:false, inList:["M","F","NM","SF", "UNK"])
    sampleAge(nullable: true)
    sampleBreed(nullable:true)
    sampleSource(blank:false)
    species(blank:false)
    labFinding(nullable:true)
    filepath(nullable:true)
    enteredby(nullable:true)
    sg(nullable:true)
    mosm(nullable:true)
    dateCreated()
}

/* This section is for static mapping to the hematology database*/
   static mapping = {
    version false
    id generator:'sequence', params:[sequence:'SHARED_SEQ']   
   }

   String toString(){
    "${sampleNameID}"
  }
}


class SampleParameter implements Comparable{


String name
String value

static hasMany = [
samples:Sample,         //domainClass1s: DomainClass1,
sampleTypes:SampleType  //domainClass2s: DomainClass2
]
static mapping = {
    version false
    id generator:'sequence', params:[sequence:'SHARED_SEQ']   
}

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

  static constraints = {
     name(blank:false)
     //value()
     //value(unique:true)
     value (unique: 'name')
 }

@Override public String toString() {
return name + " " + value
}

@Override
public int compareTo(Object o) {
    if (o == null || this == null) {
        return 0;
    } else {
        return value.compareTo(o.value)
    }
}
}
4

3 回答 3

2

作为第一个建议,当您拥有参数的 id 时,请执行以下操作。

Parameter p = Parameter.get(params.id) // or wherever your id is stored
List<Sample> samples = Sample.findAllByParameter(p) // this assumes, the parameter property is actually named 'parameter'

当然,现在没有错误处理,但你会明白的。

欢迎来到 GORM,欢迎来到 Grails。

于 2012-08-09T18:52:30.783 回答
1

问题是您没有在 executeQuery 方法中使用 HQL 查询。相反,您使用的是本机 sql。从手册:

executeQuery 方法允许执行任意 HQL 查询。

查看规范以了解执行此操作的方法。顺便说一句,这比原生 sql 更容易。

于 2012-08-09T19:42:02.173 回答
1
List<Sample> samples = Sample.findAllBySampleParameter(SampleParameter.get(variable))

试试看?

于 2012-08-09T20:56:15.227 回答