日期:2014-05-20  浏览次数:20721 次

Hibernate 自动生成主键问题.走过路过的帮忙看看,谢谢!!
数据库使用SQL   Server2000
使用Hibernate   对一个表   自动生成主键,为自增列.
数据库:
pk     pcno           int      
        pasus         varchar(1000)


映射文件:
<hibernate-mapping   package= "hibernate.dao.casus "   schema= "dbo "   catalog= "police ">
                  <class   name= "PCasus "   table= "P_Casus ">
                    <id   name= "pcno "   type= "integer "   column= "pcno ">
                            <generator   class= "identity ">
                            </generator>
                    </id>
                  <property   name= "pcasus "   length= "1000 "/>        
        </class>
</hibernate-mapping>

pojo:
public   class   PCasus     implements   java.io.Serializable   {

private   Integer   pcno;
                  private   String   pcasus;
                  //get/set方法省略
}


运行以下代码:
public   class   PCasusDao   extends   AbstractDao{

public   int   add(String   s){
Session   session   =   this.beginTransaction();
try{
PCasus   pc=new   PCasus();
pc.setPcasus(s);
session.save(pc);
this.endTransaction(true);
return   0;
}catch(Exception   e){
e.printStackTrace();
this.endTransaction(false);
return   -9;
}
     
}


public   static   void   main(String[]   arg){
PCasusDao   pd=new   PCasusDao();

System.out.println(pd.add( "asdfasdfasdgaadfasdfasdf "));
}

}
出现的问题:
Caused   by:   java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]无法将   NULL   值插入列   'pcno ',表   'police.dbo.P_Casus ';该列不允许空值。INSERT   失败。

为什么Hibernate没有自动生成主键列?
这个问题应该不复杂,我在Oracle,和mysql中都做过这样的主键自动生成操作,为什么在SQLServer2000中就有问题了列?
大家帮忙看看问题处在那里?
谢谢了~~

------解决方案--------------------
<id name= "pcno " type= "integer " column= "pcno ">
<generator class= "identity ">
</generator>
--------------------------------------
把identity改成increment就可以了.