Grails - 不能将子记录添加到父项 [英] Grails - Can't add child record to parent
问题描述
试图按照此处所示的示例进行操作我试图创建一条链接到父记录的记录。
在我的例子中,我有两个类:Sensor和Readings。我可以在没有任何问题的情况下创建传感器,但无论我如何创建读数,我似乎都会失败:(b / b)
我已经旋转了足够长的轮子,我希望能够使用JSON发布数据,但通过调试我们可以发现我的错误过程我甚至没有看JSON值,我硬编码它们,它仍然无法正常工作。
ReadingsController.groovy
包grailshelloworld
导入grails.converters.JSON
导入groovy.json.JsonSlurper
class ReadingsController {
def scaffold =读数
def save = {
def slurper = new JsonSlurper()
def结果= slurper.parseText(request.reader.text)
def s = new Sensor(sensorid:SID,sensorname:name,sensordescription:description)
.addToReadings(阅读:blah)
.save()
render([ok:false] as JSON)
}
$ / code>
sensor.groovy
package grailshelloworld
$ b $ class Sensor {
字符串sensorid
字符串sensorname
字符串sensordescription
static hasMany = [readings:Readings]
static constraints = {
sensorid blank:false,可为空:false
sensorname blank:false,可为空:false
}
}
Readings.grooovy
package grailshelloworld
import java.util.Formatter.DateTime;
class Readings {
String reading
static belongsTo = [sensor:Sensor]
}
当前错误:参数类型不匹配...
< dt> Class< / dt> ;< dd> java.lang.IllegalArgumentException< / dd>< dt> Message< / dt>>参数类型不匹配< / dd>< / dl>< h2>围绕<类= 文件名 >在grails-app /控制器/ grailshelloworld / ReadingsController.groovy< /跨度>< / H2>
< pre class =snippet>< code class =line>< span class =lineNumber> 12:< / span> def slurper = new JsonSlurper()< / code>< code class =line>< span class =lineNumber> 13:< / span> def result = slurper.parseText(request.reader.text)< / code>< code class =line>< span class =lineNumber> 14:< / span>< / code> < code class =line error>< span class =lineNumber> 15:< / span> def s = new Sensor(sensorid:& quot; SID& quot; sensorname:" name& quot; sensordescription:" description& quot;)< / code>< code class = line>< span class =lineNumber> 16:< / span> .addToReadings(阅读:& quot; blah& quot;)< / code>< code class =line>< span class =lineNumber> 17:< / span> .save()< / code>< code class =line>< span class =lineNumber> 18:< / span>< / code>< / pre>< h2> < span class =filename> PageFragmentCachingFilter.java< / span>< / h2>
您是否通过显式创建一个新的 Readings
?
def s = new Sensor(sensorid:SID,sensorname :name,sensordescription:description)
.addToReadings(new Readings(reading:'blah'))
.save()
错误提示在第15行左右,这是 def s = ...
语句的开始。
我知道文档说可以按照您尝试的方式完成 - 但值得一试。
Trying to follow the example shown here I am trying to create a record which links to a parent record.
In my case I have two classes: Sensor, and Readings. I can create Sensors without any issues, but no matter how I try to create readings I seem to fail :(
I've been spinning my wheels for long enough, I'm throwing in the towel and hoping someone can spot my silly mistake(s).
One more thing - I want to post the data using JSON. But through the debugging process I'm not even looking at the JSON values, I've hard coded them and it still doesn't work.
ReadingsController.groovy
package grailshelloworld
import grails.converters.JSON
import groovy.json.JsonSlurper
class ReadingsController {
def scaffold=Readings
def save = {
def slurper = new JsonSlurper()
def result = slurper.parseText(request.reader.text)
def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")
.addToReadings(reading: "blah")
.save()
render ([ok: false] as JSON)
}
}
sensor.groovy
package grailshelloworld
class Sensor {
String sensorid
String sensorname
String sensordescription
static hasMany = [readings: Readings]
static constraints = {
sensorid blank:false, nullable: false
sensorname blank:false, nullable: false
}
}
Readings.grooovy
package grailshelloworld
import java.util.Formatter.DateTime;
class Readings {
String reading
static belongsTo = [sensor: Sensor]
}
Current error: argument type mismatch...
<dt>Class</dt><dd>java.lang.IllegalArgumentException</dd><dt>Message</dt><dd>argument type mismatch</dd></dl><h2>Around line 15 of <span class="filename">grails-app/controllers/grailshelloworld/ReadingsController.groovy</span></h2>
<pre class="snippet"><code class="line"><span class="lineNumber">12:</span> def slurper = new JsonSlurper()</code><code class="line"><span class="lineNumber">13:</span> def result = slurper.parseText(request.reader.text)</code><code class="line"><span class="lineNumber">14:</span></code><code class="line error"><span class="lineNumber">15:</span> def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")</code><code class="line"><span class="lineNumber">16:</span> .addToReadings(reading: "blah")</code><code class="line"><span class="lineNumber">17:</span> .save()</code><code class="line"><span class="lineNumber">18:</span></code></pre><h2>Around line 195 of <span class="filename">PageFragmentCachingFilter.java</span></h2>
Have you tried it by explicitly creating a new Readings
?
def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")
.addToReadings(new Readings(reading: 'blah'))
.save()
The error is saying "around line 15" which is the start to the def s = ...
statement.
I know the docs say it can be done the way you are attempting - but it's worth a try.
这篇关于Grails - 不能将子记录添加到父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!