【问题标题】:Need help creating Grails executeQuery based on domain and lookup table需要帮助创建基于域和查找表的 Grails executeQuery
【发布时间】:2012-08-09 18:27:54
【问题描述】:

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

我能够编写一个适用于 squirrel 的 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)
    }
}
}

【问题讨论】:

  • 你能粘贴你的域类的代码吗?你说有Sample,有Paramter。 Sample 类是否有属性Parameter param?有没有三等舱?
  • 你在使用executeQuery的时候,你的hql查询具体是怎样的?
  • 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}

标签: grails hql grails-orm


【解决方案1】:

作为第一个建议,当您拥有参数的 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。

【讨论】:

  • 我试过了,现在收到一条错误消息:在 index:: 1 处缺少 IN 或 OUT 参数有什么想法吗?
【解决方案2】:

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

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

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

【讨论】:

  • 谢谢,我之前看过规范,但没有关于如何根据联接或查找表返回结果的示例。
  • 哦,好的。我认为您的问题是您的对象 Sample 无法映射,因为它的名称是小写的。
  • 顺便可以在docs.jboss.org/hibernate/orm/3.3/reference/en/html/…找到如何使用hql join
  • 我明白了。我尝试了域的大写,但它仍然说它没有映射。 ` 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")` 消息:SAMPLE 未映射 [select s.sample from SAMPLE s , SAMPLE_SAMPLE_PARAMETERS sp 其中 s.id = sp.sample_id 和 sp.sample_parameter_id = 41]
  • 我不是这个意思。对不起。如果您查看我提到的链接中的示例,您会注意到您不应该使用表的名称,而是使用您的类的名称(本例中的示例)。此外,在 HQL 中,您不需要键入连接表本身,而是通过引用的属性来建立关联。
【解决方案3】:
List<Sample> samples = Sample.findAllBySampleParameter(SampleParameter.get(variable))

试试看?

【讨论】:

  • 我试试这个,它给了我这个错误:没有找到类 [class lims.Sample] 的名称 [sampleParameter] 的属性。 Stacktrace 如下:消息:找不到类 [class lims.Sample] 的名称 [sampleParameter] 的属性 - SampleParameter 必须同时属于 Sample 和 SampleType
  • 我不确定我的问题是否表述错误,但我需要找到包含特定动态变量的样本。我想我可以通过参数获取样本,但无法通过样本获取参数。因此我发现简单地使用p.samples 会给我一个与特定参数关联的所有样本的列表。我遇到了问题,因为我是从样本端而不是参数端尝试的。
猜你喜欢
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 2020-03-06
  • 2022-12-01
  • 2012-11-12
相关资源
最近更新 更多