[size=small]org.hibernate.exception.ConstraintViolationException: could not insert
org.hibernate.exception.ConstraintViolationException: could not insert
报错原因: generator id设置问题
hibernate的映射文件bean.hbm.xml是跟库表一一对应的,如果库表中存在主键,需要自动生成就用native,有时需要在我们insert时就给他赋值,用assigned。
example:
如果是要让他自动生成的话用如下:
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native"></generator>
</id>
insert时需要为主键赋值如下:
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned"></generator>
</id>
因为采用了native的方法,底层数据库会自动生成一个long,short或者是int,如果ID设计的是用String,所以数据库无法插入的,所以如果你的数据库的ID设计的是用String型的,最好是用assinged.
<generator class=""></generator> 可以不要...
第一次Hibernate之旅经验总结
1.错误提示:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
at StudentTest.main(StudentTest.java:17)
原因:没有引入hibernate-distribution-3.6.0.Final\lib\jpa下面的hibernate-jpa-2.0-api-1.0.0.Final.jar
2.错误提示:
Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.SessionException: Session was already closed
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:320)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:589)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy0.close(Unknown Source)
at StudentTest.main(StudentTest.java:22)
原因:在程序中有这么一句Session session = sf.getCurrentSession();将其改为Session session = sf.openSession();问题解决,或者用if(session.isOpen())判断一下,具体原因不是很清楚,读者可以参考http://rmn190.javaeye.com/blog/370864
3.错误提示:
Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at StudentTest.main(StudentTest.java:21)
Caused by: java.sql.BatchUpdateException: Duplicate entry '7' for key 'PRIMARY'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
错误原因:主键的值不能重复,数据表中id=7的数据已经存在,所以导致插入数据失败!
4.错误提示(使用Session session = sf.getCurrentSession()):
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
at StudentTest.main(StudentTest.java:18)
错误原因:从3.0.1版本开始,Hibernate增加了 SessionFactory.getCurrentSession()方法。一开始,它假定了采用 JTA事务, JTA事务定义了当前session的范围和上下文(scope and context)。
错误主要原因是在hibernate.cfg.xml文件中忘记进行了如下设置:hibernate.current_session_context_class
如果是在web容器中运行hibernate,则在hibernate.cfg.xml中加入这句话:
<propertyname="hibernate.current_session_context_class">jta</property>
如果是在一个单独的需要进行JDBC连接的javaapplication中运行hibernate,则这样设置:
<propertyname="hibernate.current_session_context_class">thread</property>[/size]
org.hibernate.exception.ConstraintViolationException: could not insert
报错原因: generator id设置问题
hibernate的映射文件bean.hbm.xml是跟库表一一对应的,如果库表中存在主键,需要自动生成就用native,有时需要在我们insert时就给他赋值,用assigned。
example:
如果是要让他自动生成的话用如下:
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native"></generator>
</id>
insert时需要为主键赋值如下:
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned"></generator>
</id>
因为采用了native的方法,底层数据库会自动生成一个long,short或者是int,如果ID设计的是用String,所以数据库无法插入的,所以如果你的数据库的ID设计的是用String型的,最好是用assinged.
<generator class=""></generator> 可以不要...
第一次Hibernate之旅经验总结
1.错误提示:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
at StudentTest.main(StudentTest.java:17)
原因:没有引入hibernate-distribution-3.6.0.Final\lib\jpa下面的hibernate-jpa-2.0-api-1.0.0.Final.jar
2.错误提示:
Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.SessionException: Session was already closed
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:320)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:589)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy0.close(Unknown Source)
at StudentTest.main(StudentTest.java:22)
原因:在程序中有这么一句Session session = sf.getCurrentSession();将其改为Session session = sf.openSession();问题解决,或者用if(session.isOpen())判断一下,具体原因不是很清楚,读者可以参考http://rmn190.javaeye.com/blog/370864
3.错误提示:
Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at StudentTest.main(StudentTest.java:21)
Caused by: java.sql.BatchUpdateException: Duplicate entry '7' for key 'PRIMARY'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
错误原因:主键的值不能重复,数据表中id=7的数据已经存在,所以导致插入数据失败!
4.错误提示(使用Session session = sf.getCurrentSession()):
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
at StudentTest.main(StudentTest.java:18)
错误原因:从3.0.1版本开始,Hibernate增加了 SessionFactory.getCurrentSession()方法。一开始,它假定了采用 JTA事务, JTA事务定义了当前session的范围和上下文(scope and context)。
错误主要原因是在hibernate.cfg.xml文件中忘记进行了如下设置:hibernate.current_session_context_class
如果是在web容器中运行hibernate,则在hibernate.cfg.xml中加入这句话:
<propertyname="hibernate.current_session_context_class">jta</property>
如果是在一个单独的需要进行JDBC连接的javaapplication中运行hibernate,则这样设置:
<propertyname="hibernate.current_session_context_class">thread</property>[/size]