日期:2014-05-16  浏览次数:20944 次

困扰好几天了,Hibernate插入时报ConstraintViolationException异常
运行时控制台输出:
Hibernate: select user0_.password_md5 as col_0_0_ from t_user user0_ where user0_.user=?
Hibernate: insert into t_user (user, password_md5, name, email, phone, qq) values (?, ?, ?, ?, ?, ?)

浏览器提示的错误信息为:
{"result":"could not insert: [zhao.bbs.model.User]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [zhao.bbs.model.User]"} 

user.hbm.xml配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping >
<!-- 映射 user 表 -->
<class name="zhao.bbs.model.User" table="t_user" >
<id name="id" column="id" >
<generator class="identity"/>
</id>
<property name="user" column="user" />
<property name="password_MD5" column="password_md5"/>
<property name="name" />
<property name="email" />
<property name="phone" />
<property name="qq" />
  </class>

</hibernate-mapping>
topics.hbm.xml配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping >

<class name="zhao.bbs.model.Topic" table="t_topics" >
<id name="id" > 
<generator class="identity"/>
</id>
<property name="topic" />
<property name="postTopicTime" column="post_topic_time" />
<property name="viewCount" column="view_count" />
<property name="replyCount" column="reply_count" />
<property name="lastReplyTime" column="last_reply_time" />
<property name="topicPath" column="topic_path" />
<!--将 Topic 和 user 属性映射成为多对一关系 -->
<many-to-one name="user" column="user_id"
class="zhao.bbs.model.User" cascade="all" lazy="false">
</many-to-one>
<!--将 Topic 和lastReplyUser 属性映射成为多对一关系 -->
<many-to-one name="lastReplyUser" column="last_reply_user_id"
class="zhao.bbs.model.User" cascade="all" lazy="false">
</many-to-one>
  </class>

</hibernate-mapping>

reviews.hbm.xml配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping >
<!-- 映射review 表 -->
<class name="zhao.bbs.model.Review" table="t_reviews" >
<id name="id" > 
<generator class="identity"/>
</id>
<property name="topicId" column="topic_id" />
<property name="reviewTime" column="reiview_time"/>
<!--将 Review 和reviewUser 属性映射成为多对一关系 -->
<many-to-one name="reviewUser" column="review_user_id"
class=&quo