簡體   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