2012-08-09 37 views
1

我有兩個類:示例和參數。我也有一個sample_sample_parameter查找表,它存在以保存樣本ID和參數ID。這是映射在我的Grails應用程序。需要幫助基於域和查找表創建Grails executeQuery

我能寫,在松鼠工作的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) 
    } 
} 
} 
+0

你能否粘貼你的域類的代碼?你說有'樣品',並有'參數'。 Sample-class是否有一個「參數參數」屬性?有第三堂課嗎? – fluxon 2012-08-09 18:49:56

+0

當你使用executeQuery時,你的hql查詢具體如何? – 2012-08-09 19:19:39

+0

'def allParameters = Sample.executeQuery(「select s.sample from sample s,sample_sample_parameters sp where s.id = sp.sample_id and sp.sample_parameter_id = 41」)'其中41必須是$ {VARIABLE} – Universitas 2012-08-09 19:30:34

回答

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。

+0

我試過這和現在得到一個錯誤消息:在index :: 1缺少IN或OUT參數有什麼想法? – Universitas 2012-08-09 19:29:46

+0

p.samples的作品。 – Universitas 2012-08-27 18:56:09

+0

感謝信用! – fluxon 2012-08-29 14:11:42

1

問題是你沒有在你的executeQuery方法中使用HQL查詢。相反,您使用的是原生sql。從手冊:

The executeQuery method allows the execution of arbitrary HQL queries.

看看在specification看到這樣做的方式。順便說一下,它比原生SQL更容易。

+0

謝謝,我查看過以前的規範,但沒有關於如何根據連接或查找表返回結果的示例。 – Universitas 2012-08-09 20:02:30

+0

哦,好的。我認爲你的問題是你的對象Sample無法映射,因爲它的名字是小寫。 – 2012-08-09 20:05:13

+0

順便說一下,你可以在http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins – 2012-08-09 20:07:40

1
List<Sample> samples = Sample.findAllBySampleParameter(SampleParameter.get(variable)) 

試試看吧?

+0

我試試這個,它給了我這個錯誤:找不到名爲[sampleParameter]的類[class lims.Sample]的屬性。Stacktrace如下:消息:找不到名爲[sampleParameter]的類的屬性[class lims.Sample] - SampleParameter必須同時屬於Sample和SampleType – Universitas 2012-08-13 14:37:33

+0

我不確定我是否說錯了我的問題,但我需要的是找到包含特定動態變量的樣本。我想我可以通過參數進行採樣,但不能通過採樣得到參數​​。因此我發現僅僅使用'p.samples'就會給我一個與特定參數相關的所有采樣的列表。我遇到了問題,因爲我正在嘗試從樣本側,而不是參數側。 – Universitas 2012-08-13 19:13:34