Grails与具有String id列的域的多对多关系

时间:2016-12-05 15:22:59

标签: grails many-to-many gorm

如何与id列为字符串的域创建多对多关系?

UnitObj 值示例为:'0','1','001','002','1234'等。 因此,由于这些值,我无法将id列(UnitObj中的值列) long integer

任何帮助将不胜感激。 以下是域名:

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"
    }
}

---------的修改 --------

如果我将字符串更改为UnitObj,则在尝试创建UserUnitObj时,它会给出:

  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 个答案:

没有答案