Microsoft SQLデータベース内に既存のテーブルを管理するためにGrailsを使用しようとしています。問題は、テーブルにint IDカラムがないことです。それが主キーとして持っているものはStringフィールドです。私のドメインクラスでGrailsはId以外のID名を持つ行を更新できません
が、私はこの仕事をしなければならない次のコードを、持っている:
static mapping = {
id name: "myPK", generator: "assigned", column: "myPK"
version false
}
を、これは更新関数である:あなたが見ることができるように
@Transactional
def update(MyDomain myDomain) {
if (myDomain== null) {
transactionStatus.setRollbackOnly()
notFound()
return
}
if (myDomain.hasErrors()) {
transactionStatus.setRollbackOnly()
respond myDomain.errors, view:'edit'
return
}
myDomain.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: 'myDomain.label', default: 'MyDomain'), myDomain.myPK])
redirect myDomain
}
'*'{ respond myDomain, [status: OK] }
}
}
、私は避難所」 idを 'myPK'に置き換えるのではなく、実際に生成されたものから大幅に変更されました。私はidを置き換えずにこれを試してみました。
この
は私が取得エラーメッセージです:MyDomain not found with id myString
私もそれは新しいエントリを保存するように作用することで、新しいものを作成し、問題が生じていますが、それにリダイレクトすることはできません、それは存在しません。データベースに格納されます。私はそれから選択すると、すべてのエントリを参照してください。
私はあなたが見たい他のコードがある場合教えてくださいGrailsの3.1.1
を使用しています。
編集1
ここでは、完全なドメインクラスの:
class MyDomain{
String myPK
Character prop1
Character prop2
Character prop3
Character prop4
Character prop5
Character prop6
Character prop7
Character prop8
Character prop9
Character prop10
Character prop11
Date prop12
Character prop13
static mapping = {
id name: "myPK", generator: "assigned", column: "myPK"
version false
}
static constraints = {
myPKmaxSize: 10
prop1 nullable: true, maxSize: 1
prop2 nullable: true, maxSize: 1
prop3 nullable: true, maxSize: 1
prop4 nullable: true, maxSize: 1
prop5 nullable: true, maxSize: 1
prop6 nullable: true, maxSize: 1
prop7 nullable: true, maxSize: 1
prop8 nullable: true, maxSize: 1
prop9 nullable: true, maxSize: 1
prop10 nullable: true, maxSize: 1
prop11 nullable: true, maxSize: 1
prop12 nullable: true
prop13 nullable: true, maxSize: 1
}
}
編集2 ここでは、私は、ドメインクラスの新しいエントリを作成するために使用しています保存機能である:
@Transactional
def save(MyDomain myDomain) {
if (myDomain== null) {
transactionStatus.setRollbackOnly()
notFound()
return
}
if (myDomain.hasErrors()) {
transactionStatus.setRollbackOnly()
respond myDomain.errors, view:'create'
return
}
myDomain.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'myDomain.label', default: 'MyDomain'), myDomain.myPK])
redirect myDomain
}
'*' { respond myDomain, [status: CREATED] }
}
}
ドメインの残りの部分、具体的にはプロパティを確認すると便利です。また、ドメインを新しくする方法を示すことができますか? –
ドメインクラス全体を追加しました。ドメインを新しくすることはどういう意味ですか?どのように作成したのかを知っていれば、答えはdb-reverse-engineerを使用したことです。 –
私はあなたの質問のこの部分に焦点を当てています。「新しいエントリを保存したように振る舞いますが、リダイレクトできず、データベースにも存在しません。 新しいものを作成すると、何をしていますか? 'def myDomain = new MyDomain()'?コンストラクタでkey:valueのペアを使用してプロパティを設定しているか、それを新しくした後にプロパティを設定していますか? –