hibernate是在给具体的类写映射文件的时候设定主键的生成策略的
一般使用native就可以了,可以让你所使用的数据库自动为你的表的主键插入数值,不同数据库的实现方式不同

解决方案 »

  1.   

    这个就要看你的数据库了,mysql的字段有autoincreasement属性,oracle使用SEQ的
      

  2.   

    <?xml version="1.0" encoding='UTF-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
                                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="cn.edu.tju.kse.bean">    <class name="Results" table="Results">
            <id name="id" column="id" type="integer">
                <generator class="native"/>
            </id> 
            <property name="titles" column="titles" type="string"  not-null="true" />
            <property name="files" column="files" type="string"  not-null="false" />
            <property name="sorts" column="sorts" type="string"  not-null="true" />
            <property name="userId1" column="userId1" type="string"  not-null="false" />
            <property name="userId2" column="userId2" type="string"  not-null="false" />
            <property name="userId3" column="userId3" type="string"  not-null="false" />
            <property name="isVisual" column="isVisual" type="int"  not-null="true" />
        </class>
        
    </hibernate-mapping>
      

  3.   

    你可以看看hibernate in action。
    里面对主键生成方式介绍的很详细。