困扰好几天了,Hibernate插入时报ConstraintViolationException异常

qqliang1314 qqliang1314 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
运行时控制台输出:
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="zhao.bbs.model.User" cascade="all" lazy="false">
</many-to-one>
</class>

</hibernate-mapping>

MySql数据库中表t_topics的两个外键均为 t_user表的id;表t_reviews的外键也是 t_user表的id
到底是哪出问题了呢
...全文
评论 4
收藏 已收藏
分享
返回顶部
qqliang1314
MySQL 57,062 成员 56,759 内容