Java 未创建自动增量

Java 未创建自动增量,java,hibernate,grails,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: [

域类

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
    }
}
这个类创建一个表
我需要创建一个字段id为自动递增的表。帮助我进行映射。

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

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

    .....  

  </class>  
 </hibernate-mapping> 

这里
strategy=GenerationType.AUTO
如果您使用的是Grails,并且您对名为id的主键感到满意,那么它将把id列设置为
自动递增
,您不需要指定任何指定的信息。Grails将负责基于底层数据库的自动增量策略

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

您已指定可以使用空名称,但该名称可能不正确,因为您将得到只包含主键的行

您还应该为您的表使用一个非多元化的名称,即team

package testgrails12

    class Team {

    String name

    static mapping = {
        version false
    }

    static constraints = {
        name nullable: true
    }
}

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

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

您使用的是
hbm.xml
文件还是
Annotation
?我可以写入和读取数据,但不能更新或删除数据。函数
delete()
不起作用。我没有看到任何错误(这使任务变得非常复杂)。我在数据库中看到的结果是xml映射。这不适合我
@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;
    }