2011-06-24 9 views
0

新しい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) 
+4

使用しているような文字列連結は非常に危険で、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 –

答えて

1

あなたが直接その後、最も簡単で効率的な方法をIDを使用することができる場合さ

+0

のドキュメントをご覧ください。ありがとう、それは働いた! :-) – nano7

2

の代わりに:-)ありがとうCorrect.get(id)に電話してください。

それ以外の場合は、findByダイナミックドメインメソッドを参照してください。

0

あなたのドメインクラスの構造は見えませんが、私はcorrectanswers.answera()の最後から '()'を削除すると思います。それは方法ではないので動作するはずです。私は、1つの「ansera」ドメインクラスとして「正しい人」と仮定しています。

def correctanswers = Correct.executeQuery("from Correct answers where answers.lpicid=" + lpicid); 

def correcta = correctanswers.answera; 
関連する問題