2016-12-05 9 views
0

id列が文字列のドメインと多対多リレーションシップを作成する方法はありますか?Grails多対多リレーションシップ(String ID列を持つドメイン)

UnitObj値の例である: '0'、 '1'、 '001'、 '002'、 '1234' 等 だからidカラム(UnitObjの値列)長くすることはできませんまたは整数これらの値のためです。

ご協力いただければ幸いです。ここ はドメインである:ここでは

class UnitObj { 
    String value 
    String unit 
    UnitObj parent 

    static constraints = { 
     value(unique: true, nullable: false) 
     unit(nullable: false) 
     parent(nullable: true) 
    } 

    static mapping = { 
     version false 
     id generator: 'assigned', name: "value", type: 'string' 
     table name: "unit_obj", schema: "db" 
    } 
} 

は、私が変更したいものです。

class UserUnitObj { 
    User user 
    String unit //This should be UnitObj not string 
    String subUnit //This should be UnitObj not string 

    static constraints = { 
     user(nullable: false) 
     unit(nullable: false) 
     subUnit(nullable: true) 
    } 
    static mapping = { 
     version false 
     table name: "user_unit_obj", schema: "db" 
    } 
} 

--------- EDIT --------

UserUnitObjを作成しようとしたとき、私は、UnitObjに文字列を変更する場合、それは与える:

UserUnitObj u = new UserUnitObj() 
    u.user = User.get(userUnitJson.user) 
    u.unit = UnitObj.findByValue(userUnitJson.unit.id.toString()) 
    u.subUnit = UnitObj.findByValue(userUnitJson.subUnit.id.toString()) 
    u.save(flush: true) 

これはエラーです:

2016-12-05 17:51:14,305 [http-bio-8080-exec-4] ERROR spi.SqlExceptionHelper - ERROR: column "sub_unit_id" is of type bigint but expression is of type character varying 

Hint: You will need to rewrite or cast the expression. 
    Position: 79 
Error | 
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute statement; bad SQL grammar [n/a]; nested exception is org.postgresql.util.PSQLException: ERROR: column "sub_unit_id" is of type bigint but expression is of type character varying 
    Hint: You will need to rewrite or cast the expression. 
+0

これらの文字列を 'UnitObj'インスタンスに変更すると、どうなりますか? – Gregg

+0

@Gregg投稿を編集しました – trd3v3lop

答えて

0

問題は、あなたがを使用していることではありません割り当てられたIDについてはを参照してください。問題は、UserUnitObjに、idの代わりにvalueを使用していると伝えていないためです。デフォルトでは、GORMはidという名前のカラムに常にFKを割り当てます。あなたが何か違うことをしたら、あなたはそれを伝えなければなりません。方法については、documentation hereを参照してください。

+1

ありがとうございました:) – trd3v3lop

関連する問題