java - 组织.postgresql.util.PSQLException : ERROR: null value in column "category_id" violates not-null constraint

标签 java spring postgresql jpa parent-child

我有一个奇怪的问题:我正在尝试在我的数据库中保存一个用户,这个用户有一个技能列表。这些技能已经在具有链接类别的数据库中,并且类别具有链接域。结构如下所示:enter image description here

当我打印申请人的技能列表时,我有这个:

skills=[Skill{categories=[Category{domains=[Domain{id=4, name=DevOps}], id=13, name=BackEnd}], id=23, name=Java}, Skill{categories=[Category{domains=[Domain{id=4, name=DevOps}], id=13, name=BackEnd}], id=24, name=C}],

这是在申请人和技能之间建立联系的表格:

enter image description here

但是当我试图挽救申请人时,我得到了这个 Détail:失败行包含 (23, null, null, 499)。有人可以解释我吗?我正在使用 jpa annotaions 开发一个 spring 应用程序。

编辑 1:

申请人实体:

@Entity
@Table(name = "ATS_APPLICANT")
public class ApplicantEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "home", unique = false, nullable = true)
    private Boolean home;

    @Column(name = "anonymous", unique = false, nullable = true)
    private Boolean anonymous;

    @Enumerated(EnumType.STRING)
    @Column(name = "job_type")
    private JobType jobType;

    @Column(name = "min_salary", unique = false, nullable = true)
    private Integer minSalary;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(unique = true)
    private UserEntity user;

    @OneToMany(targetEntity = ApplicantWorkExperienceEntity.class, cascade = CascadeType.ALL)
    private List<ApplicantWorkExperienceEntity> applicantWorkExperiences = new ArrayList<ApplicantWorkExperienceEntity>();

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinTable(name = "ATS_APPLICANT_SKILL", joinColumns = @JoinColumn(name = "applicant_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "skill_id", referencedColumnName = "id"))
    private List<SkillEntity> skills = new ArrayList<SkillEntity>();

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "ATS_APPLICANT_LOCATION", joinColumns = @JoinColumn(name = "applicant_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "location_id", referencedColumnName = "id"))
    private List<LocationEntity> locations = new ArrayList<LocationEntity>();

技能实体:

@Entity
@Table(name = "ATS_SKILL")
public class SkillEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "name", length = 42, nullable = false, unique = true)
    private String name;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "ATS_SKILL_CATEGORY", joinColumns = @JoinColumn(name = "skill_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"))
    private List<CategoryEntity> categories = new ArrayList<CategoryEntity>();

类别实体:

@Entity
@Table(name = "ATS_CATEGORY")
public class CategoryEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "name", length = 42, nullable = false, unique = true)
    private String name;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "ATS_CATEGORY_DOMAIN", joinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "domain_id", referencedColumnName = "id"))
    private List<DomainEntity> domains = new ArrayList<DomainEntity>();

编辑 2: 域实体:

@Entity
@Table(name = "ATS_DOMAIN")
public class DomainEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "name", length = 42, nullable = false, unique = true)
    private String name;

最佳答案

我通过在技能上使用@ElementCollection 注释获得了成功。

关于java - 组织.postgresql.util.PSQLException : ERROR: null value in column "category_id" violates not-null constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051740/

相关文章:

java - 是否可以像单选按钮那样使 Imageview 具有选中和未选中状态?

mysql - 在 SQL 中查询任意 x 处的 y 值的 XY 数组对

java - 挑战 token 错误

java - Cucumber 和 WebDriver,不需要的时候打开 chrome

java - 由于 UnsupportedClassVersionError,使用 JDK 6 的 Maven 构建失败

java - 使用 DynamoMapper 和类 Annotation 创建具有全局二级索引的表

spring - 在 Spring Redis 中设置 "key"

java - 提出多部分请求不起作用

java - 如何使用 Rest 模板检查端点的运行状况

sql - PostgreSQL 数据库 - 仅在记录不存在时插入