【问题标题】:org.hibernate.exception.ConstraintViolationExceptionorg.hibernate.exception.ConstraintViolationException
【发布时间】:2011-08-14 13:07:50
【问题描述】:

我正在使用this 链接制作示例程序 我在 sqlserver 中有以下表格:

`CREATE TABLE Student2(
STUDENT_ID int NOT NULL,
LastName varchar(255) NOT NULL,
STUDENT_NAME varchar(255),
STUDENT_ADDRESS int,
UNIQUE (STUDENT_ADDRESS))

CREATE TABLE Address2(
ADDRESS_ID int NOT NULL,
LastName varchar(255) NOT NULL,
ADDRESS_STREET varchar(255),
ADDRESS_CITY varchar(255),
ADDRESS_STATE varchar(255),
ADDRESS_ZIPCODE varchar(255))

我在 Student.hbm.xml 中输入了studentId int

<id name="studentId" type="int" column="STUDENT_ID">
    <generator class="native" />
</id>

我在 Address.hbm.xml 中取了 addressId type int:

<id name="addressId" type="int" column="ADDRESS_ID">
    <generator class="native" />
</id>

我面临以下异常:

org.hibernate.exception.ConstraintViolationException: could not insert: [com.vaannila.student.Address]`
exception:`org.hibernate.exception.ConstraintViolationException: could not insert: [com.vaannila.student.Address]
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'ADDRESS_ID', table 'sample.dbo.Address2'; column does not allow nulls. INSERT fails.

请帮忙。

【问题讨论】:

    标签: hibernate hibernate-mapping


    【解决方案1】:

    您的表没有自动分配的主键。实际上,您的创建表根本没有定义主键。

    下面是 MySql 创建带有主键的表的示例,该表可与本机生成器配置一起使用:

    创建表asdf(pk整型主键auto_increment)

    你可以删除你的表并让 hibernate 来创建它们。

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 2017-12-17
      • 1970-01-01
      • 2018-09-13
      • 2016-03-28
      • 2019-07-18
      • 2018-05-14
      • 2017-07-12
      相关资源
      最近更新 更多