小弟最近在使用hibernate做一个简单实践的时候出现一个非常奇怪的问题,先将我本机环境说一下myeclipse+mysql5
没有使用中间件,插入数据和得到数据显示,我是直接在myeclipse控制台打印出来的。1.在mysql上建立表
-- Table "student" DDL
CREATE TABLE `student` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) DEFAULT NULL,
  `sex` varchar(10) DEFAULT NULL,
  `age` int(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=gbk;2.工程加入hibernate支持,在hibernate 的date视图上直接将表反转生成相关代码,唯一修改代码的Student.hbm.xml 内容如下:
<hibernate-mapping>
    <class name="dao.Student" table="student" catalog="db">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="userName" length="100" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" length="10" />
        </property>
    </class>
</hibernate-mapping>
3.我写了一个类来验证studentDao的 save()方法package test;import java.util.List;import org.hibernate.Transaction;import dao.Student;
import dao.StudentDAO;public class testDate { /**
 * @param args
 */
public static void main(String[] args) {
StudentDAO dao = new StudentDAO();
Student stu  = new Student();
stu.setUserName("wwww");
stu.setSex("女");
stu.setAge(24);

Transaction t = dao.getSession().beginTransaction();
dao.save(stu);
t.commit();

//StudentDAO dao = new StudentDAO();
List<Student> stus = dao.findAll();

for(Student st: stus){
System.out.println(st.getId());
System.out.println(st.getUserName());
} }}
4.如果mysql数据库没有数据的话插入正常,如果有数据则提示如下错误log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        max(id) 
    from
        student
Hibernate: 
    insert 
    into
        db.student
        (userName, age, sex, id) 
    values
        (?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.testDate.main(testDate.java:24)
Caused by: java.sql.BatchUpdateException: Duplicate entry '3' for key 'PRIMARY'
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:853)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more
请问何解???求达人赐教,在线等啊!!!!!!!

解决方案 »

  1.   

    不可能有人回答你,CSDN上的人都很懒,你那么长除非那人整天无所事事就像我一样
      

  2.   

    Caused by: java.sql.BatchUpdateException: Duplicate entry '3' for key 'PRIMARY'这个原因,你翻译下吧,好像是主键为3的重复了。。
      

  3.   

    如果是主建重复的话,我把主建和数据库ID自动增加,结果每次插入的数据都是ID=3,为何啊?
    貌似hibernate每次写进去的ID不是在数据库ID取最大值加1的方法insert的
      

  4.   

    正因为有1楼这样的,CSDN才会火下去嘛
      

  5.   

    换一下主键生成方式如下
            <id name="id" column="id" type="integer">
                <generator class="native" />
            </id>4) increment
    主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持
    一个变量,以保存着当前的最大值,之后每次需要生成主键的时候
    将此值加1作为主键。
    这种方式可能产生的问题是:如果当前有多个实例访问同一个数据
    库,那么由于各个实例各自维护主键状态,不同实例可能生成同样
    的主键,从而造成主键重复异常。因此,如果同一数据库有多个实
    例访问,此方式必须避免使用。7) native
    由Hibernate根据底层数据库自行判断采用identity、hilo、sequence
    其中一种作为主键生成方式。
      

  6.   

    Transaction t = dao.getSession().beginTransaction();
    Student stu  = new Student();
    stu.setUserName("wwww");
    stu.setSex("女");
    stu.setAge(24);
    dao.save(stu);
    t.commit();
    红色的放上面试试
      

  7.   

      
    <id name="id" column="id" type="integer">
          <generator class="native" />
            
    </id>非常感谢,修改这个后就正常了