简体   繁体   中英

org.postgresql.util.PSQLException: ERROR: null value in column “category_id” violates not-null constraint

i have a weird issue : i'am trying to save a User in my DB, this user has a list of skills. those skills are already in the db with linked categories and categories have linked domains. the structure looks like this : 在此处输入图片说明

when i print the list of skills from the applicant i have this :

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

and here is the table making the link between Applicant and skills :

在此处输入图片说明

but when i'm trying to save the applicant i have this Détail : Failing row contains (23, null, null, 499). can someone explain me ? I'm working on a spring application using jpa annotaions.

EDIT 1 :

ApplicantEntity :

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

SkillEntity :

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

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

EDIT 2 : 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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM