未创建自动增量

时间:2017-03-31 10:14:02

标签: java hibernate grails

域类

package testgrails12

class Teams {
    Integer id
    String name
    static mapping = {
        table 'teams'
        version false
        name column: 'name', sqlType: 'VARCHAR(200)'
        id generator: 'increment',
                params: [table:'teams', column: 'idteam', sqlType: 'INT(10)', updateable: false, insertable: false]
        /*id column: 'idteam', sqlType: 'INT(11)', updateable: false, insertable: false,
                generator: 'increment'*/
    }
    static constraints = {
        name nullable: true
    }
}

此类创建一个表 img 我需要创建一个具有字段ID自动增量的表。帮帮我映射。

3 个答案:

答案 0 :(得分:1)

如果您正在使用Grails并且您对名为id的主键感到满意,则不需要指定您指定的任何信息。 Grails将根据您的基础数据库处理自动增量策略。

如果您对表格名称小组感到满意,那么您也不需要为与此相关的映射添加任何内容。

您已经指定您可以使用可能不正确的空名称,因为您最终只会使用主键。

您还应该为您的表格(即团队)使用非复数名称。

package testgrails12

    class Team {

    String name

    static mapping = {
        version false
    }

    static constraints = {
        name nullable: true
    }
}

答案 1 :(得分:0)

在您的映射文件中,您需要添加这样的生成器。

<hibernate-mapping>  
  <class ...>  
    <id ...>  
     <generator class="increment"></generator>  
    </id>  

    .....  

  </class>  
 </hibernate-mapping> 

上面你必须只为这个id做映射。因为在Hibernate中默认生成器是assigned。所以你需要添加你的发电机。

如果要在类中使用注释,则需要使用id属性添加这些注释。

@Id  
@GeneratedValue(strategy=GenerationType.AUTO)  
private int id; 

此处strategy=GenerationType.AUTO会将ID列设为Auto Increment

答案 2 :(得分:0)

如果您使用的是注释,则下面的代码段可能会对您有所帮助:

Convert.ToDecimal(d)