我是用session的.saveorUpdate(对象);但是那个表中存在一个自动增长的ID(主键),用这个也会写入那个ID,我也没对ID赋值。请问如何保存一个包含自动增长的主键的表?

解决方案 »

  1.   

    将表名.hbm.xml中的对应标识符的generator属性修改一下,例如:
    <id name="id", type="long" column="ID">
     <meta attribute="scope-set">private</meta>
     <generator class="increment:/>
    </id>再修改对应的表名.java文件
    private Long id;public Long getId(){
      return this.id;
    }private void setId(Long Id){  //注意,这里是private类型
      this.id=id;
    }
      

  2.   

     打错了,应该是<generator class="increment"/ > 
      

  3.   

    xml里面是
    <id name="provinceId" type="integer">
                <column name="ProvinceID" />
                <generator class="increment" />
            </id>java里面也改成了private,可还是不行
      

  4.   

    报错信息有:Hibernate: insert into Audit.dbo.Province (ProvinceName, ProvinceID) values (?, ?)
    2007-10-22 10:46:44,390 - org.hibernate.util.JDBCExceptionReporter -0    [http-8080-2] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 544, SQLState: 23000
    2007-10-22 10:46:44,406 - org.hibernate.util.JDBCExceptionReporter -16   [http-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter  - 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'Province' 中的标识列插入显式值。
    2007-10-22 10:46:44,437 - org.hibernate.event.def.AbstractFlushingEventListener -47   [http-8080-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
    org.hibernate.exception.ConstraintViolationException: could not insert: [com.audit.domain.Province]Caused by: java.sql.SQLException: 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'Province' 中的标识列插入显式值。2007-10-22 10:46:44 org.apache.catalina.core.ApplicationDispatcher invoke
    严重: Servlet.service() for servlet jsp threw exception
    java.lang.NullPointerException
      

  5.   

    你要做update()的话,一定要先取出这个对象了,然后修改id以外的属性,再update()就应该是没有问题的。你不能拿一个游离的对象(也就是你在程序里面new()的对象)去update()一个现成的对象,这个就算是update()了。
      

  6.   

    你定义vo对象的时候不要给它的id赋值