新しいGrailsを使用しています。データベースエントリを取得しようとしています。 データベース接続は問題なく動作します。その構造があるので、アイブ氏は、正解(ブール値)が含まれ、「正しい」という名前のクラスを持ってデータベースにHibernateを使ってGrailsのカラムからデータベース値を問い合わせる
:
ID |バージョン|アンサー| ... |答えlpicid
lpicidの質問への参照。各lpicidには1つのエントリしかありません。主キーはidです。
今、私は正しい答えを呼び出し、変数で回答を保存したいとき、このdoesntのは本当に仕事:
def correctanswers = Correct.executeQuery("from Correct answers where answers.lpicid=" + lpicid);
def correcta = correctanswers.answera();
私は、データベースのエントリを取得し、私は(lpicidだけでデフ数である)ことがわかりますcorrectanswers.answera()は機能しません。私はいつもこのメッセージを受け取ります:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.answera() is applicable for argument types:() values: []
Possible solutions: inspect(), clear(), clear(), clear(), any(), asList()
at com.lpic.LpicSimulatorController$_closure2.doCall(LpicSimulatorController.groovy:76)
at com.lpic.LpicSimulatorController$_closure2.doCall(LpicSimulatorController.groovy)
at java.lang.Thread.run(Thread.java:662)
答えを得るために私はこれを解決できますか?
def correctanswers = Correct.executeQuery("from Correct answers where answers.lpicid=" + lpicid);
あなたが行うことはできません:
def correctanswers = Correct.findByLpicid(lpicid)
使用しているような文字列連結は非常に危険で、SQLインジェクション攻撃を許可することができます。 'get()'は正解ですが、一般的に 'Correct.executeQuery("正しい答えはanswers.lpicid = "+ lpicid")は 'Correct.executeQuery("正しい回答からはanswers.lpicid =? 、[lpicid]) 'または' Correct.executeQuery( "正しい答えからのanswers.lpicid =:id"、[id:lpicid]) 'である。 http://grails.org/doc/latest/ref/Domain%20Classes/executeQuery.html –