繁体   English   中英

未创建自动增量

[英]Auto-increment not created

域类

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自动递增的表。 帮我映射。

如果您使用的是Grails,并且对名为id的主键感到满意,则无需指定任何已指定的信息。 Grails将根据您的基础数据库照顾自动增长策略。

同样,如果您对表名称团队感到满意,则无需在映射中添加任何与此相关的内容。

您已经指定可以使用空名称,因为您可能会得到仅带有主键的行,因此名称可能不正确。

您还应该为表使用非复数名称,即团队名称。

package testgrails12

    class Team {

    String name

    static mapping = {
        version false
    }

    static constraints = {
        name nullable: true
    }
}

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

<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

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

@Id @GeneratedValue(strategy=GenerationType.AUTO) 
OR 
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) // both are the same thing
    @Column(name = "Id", unique = true, nullable = false)
    public Long getId() {
        return this.Id;
    }

    public void setId(Long Id) {
        this.Id = Id;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM