【问题标题】:could not execute statement; SQL [n/a]; constraint [email_address\" of relation \"users]; nested exception无法执行语句; SQL [不适用];关系 \"users] 的约束 [email_address\";嵌套异常
【发布时间】:2021-05-26 13:07:24
【问题描述】:

我在添加到候选人表时遇到此错误。候选人表扩展了用户表。邮箱和密码列来自这个表。我觉得是数据库有问题,我用的是postreSql。会是什么原因?

用户表

package com.example.hrmsdemo.entities.concretes;

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name="users",uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
@Inheritance(strategy = InheritanceType.JOINED)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private int id;

    @Column(name = "email")
    @NotNull
    private String email;

    @NotNull
    @Column(name = "password")
    private String password;
}


候选人表

package com.example.hrmsdemo.entities.concretes;

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name="candidates",uniqueConstraints = {@UniqueConstraint(columnNames = {"identity_number"})})
@EqualsAndHashCode(callSuper = true)
@Entity
public class Candidate extends User {


    @Column(name = "first_name")
    @NotNull
    private String first_name;

    @NotNull
    @Column(name = "last_name")
    private String last_name;

    @NotNull
    @Column(name = "identity_number")
    private String identity_number;

    @NotNull
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "birth_date")
    private Date birth_date;


}

错误

2021-05-26 15:59:53.230  WARN 24964 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
2021-05-26 15:59:53.230 ERROR 24964 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : HATA: null value in column "email_address" of relation "users" violates not-null constraint
  Ayrıntı: Hata veren satır (7, null, 123414, veyselhim@gmail.com) içeriyor.
2021-05-26 15:59:53.245 ERROR 24964 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [email_address" of relation "users]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

【问题讨论】:

  • 为什么要在 PosgreSQL 问题中添加 Microsoft SQL Server 标记?我删除了不适当的标签并添加了正确的标签,以提高您获得答案的机会。
  • 谢谢先生,我没看到

标签: java spring postgresql hibernate


【解决方案1】:

我假设您真的将User#email 映射到名为email_address 的列?那是数据库抱怨的列,但您没有显示到该列的映射。

假设这是真的,你已经定义了这个属性应该是非空的,这似乎也是数据库的定义方式。这表明您正在尝试保存用户并且没有设置其电子邮件属性。

或者也许你真的没有映射那个列,所以 Hibernate 从不尝试写入它。很难从你给我们的东西上看出来。

【讨论】:

  • 数据库里的名字和我在代码里写的名字不一样,谢谢
猜你喜欢
  • 2019-06-06
  • 2018-07-07
  • 2019-11-15
  • 2017-01-11
  • 2019-07-24
  • 2020-02-12
  • 2019-11-28
  • 2022-11-09
  • 1970-01-01
相关资源
最近更新 更多