当前位置: 首页 > 知识库问答 >
问题:

psqlException:错误:列“category_id”中的null值违反了not-null约束

隗和裕
2023-03-14

当我打印申请者的技能列表时,我有以下内容:

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}],

下面是一张表格,列出了申请人和技能之间的联系:

但是当我试图保存申请者时,我有这样一个提示:失败的行包含(23,null,null,499)。有人能给我解释一下吗?我正在使用jpa注释开发一个spring应用程序。

@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>();
@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;

共有1个答案

严瑞
2023-03-14

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

 类似资料: