<id name="SId" type="java.lang.Integer">
            <column name="s_id" />
            <generator class="native" />
</id>怎样让主键SID从2004182401开始自增?,每次增加1,效果是;2004182401 2004182402  2004182403 ......
默认时只能从1开始
用到的数据库是MySQL

解决方案 »

  1.   

    看你用的什么数据库 一般ORACLE 和MYSQL都可以让主键从一个数字开始递增的 你自己可以去找一下 
      

  2.   

    如果不是数字类型,那就写个自定义函数,然后对该字段写默认值即可。 
    例如:create function f_max()
    returns char(4)
    as
    begin
        declare @s int
        select @s = isnull(max( cast(id as int)),0) + 1 from ta
        return right('0000'+ ltrim(@s),4)
    end
    go
      
    create table ta(ID char(4) default (dbo.f_max())  primary key,col int) 
    goinsert ta(col) select 2
    insert ta(col) select 3
    select * from ta
    /*
    ID   col         
    ---- ----------- 
    0001 2
    0002 3(所影响的行数为 2 行)
    */
    drop table ta
    drop function f_max
      

  3.   

    不能在映射文件里写个默认的值吗?我的主键是java.lang.Integer 类型的
    你写的是一个存储函数吗?它的作用我有点看不懂,还请详细指教.
      

  4.   

    如果是oracle的话,是可以设置sequence的值的吧
      

  5.   

    alter table table_name auto_increment=2004182401;
      

  6.   


    CREATE TABLE `test` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2004182401 DEFAULT CHARSET=utf8;
      

  7.   


    INT 可能满足不了, 用BIGINT
      

  8.   

    这样建表确实可以让表的ID从2004182401开始,但是我说的是在用hibernate的映射文件中怎么设置,
    我做项目的时候建表,就已经用了和你一样的方式,但把表反转(Hibernate Reverse Engineering)成实体类和映射文件后,就只能从1开始递增了