简体   繁体   English

未创建自动增量

[英]Auto-increment not created

domain class 域类

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

This class creates a table 此类创建表 img I need to create a table with a field id auto incremental. 我需要创建一个字段ID自动递增的表。 Help me whith mapping. 帮我映射。

If you're using Grails and you're happy with the primary key named id you don't need to specify any of the info you've specified. 如果您使用的是Grails,并且对名为id的主键感到满意,则无需指定任何已指定的信息。 Grails will take care of the auto-increment strategy based on your underlying DB. Grails将根据您的基础数据库照顾自动增长策略。

Again if you're happy with the table name team you don't need to add anything to mapping related to this. 同样,如果您对表名称团队感到满意,则无需在映射中添加任何与此相关的内容。

You've specified you're okay having a null name which may not be correct as you'll end up with rows with just a primary key. 您已经指定可以使用空名称,因为您可能会得到仅带有主键的行,因此名称可能不正确。

You should also go with a non-pluralised name for your table ie team. 您还应该为表使用非复数名称,即团队名称。

package testgrails12

    class Team {

    String name

    static mapping = {
        version false
    }

    static constraints = {
        name nullable: true
    }
}

In you mapping file you need to add generator like this. 在映射文件中,您需要像这样添加生成器。

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

    .....  

  </class>  
 </hibernate-mapping> 

above you have to do mapping for id like this only. 上面,您只需要像这样对id进行映射。 Because in Hibernate default generator is assigned . 因为在Hibernate中默认assigned生成器。 So you need to add your generator. 因此,您需要添加发电机。

If you want to use Annotation in class than you need to add those annotation with you id property. 如果要在类中使用注释,则需要使用id属性添加这些注释。

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

Here strategy=GenerationType.AUTO it will make id column to Auto Increment . 在这里strategy=GenerationType.AUTO它将id列设置为Auto Increment

If you are using annotation then below code snippet might help you: 如果您使用注释,则下面的代码片段可能会帮助您:

@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