有没有一种方法可以创建一个用外键的对象,然后在Spring Boot JPA中将其设置为null?

发布于 2025-02-04 04:52:39 字数 4431 浏览 1 评论 0原文

我在两个类之间有一对一的关系。我想创建阶段项目,而不必将候选ID插入它,因为之后我会得到候选人,因此基本上不存在。

现在,我遇到了错误:

无法执行语句; sql [n/a];约束[null];

因为我没有向候选人发送。

这是第一类:

public class PhaseItems {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "phaseI_id")
private long id;

private String PhaseItem;

@ManyToMany(fetch = FetchType.LAZY,
        mappedBy = "items")
@JsonIgnore
private List<PhaseTemplate> template = new ArrayList<>();

 @OneToOne(fetch = FetchType.LAZY, optional = true)
 @JoinColumn(name = "candidate_id", nullable = true)
 private Candidate candidate;
 
 public PhaseItems() {
    super();
 }  



public PhaseItems(long id, String phaseItem) {
    super();
    this.id = id;
    PhaseItem = phaseItem;
}



public long getId() {
    return id;
}


public void setId(long id) {
    this.id = id;
}


public String getPhaseItem() {
    return PhaseItem;
}


public void setPhaseItem(String phaseItem) {
    PhaseItem = phaseItem;
}







public List<PhaseTemplate> getTemplate() {
    return template;
}



public void setTemplate(List<PhaseTemplate> template) {
    this.template = template;
}



public Candidate getCandidate() {
    return candidate;
}



public void setCandidate(Candidate candidate) {
    this.candidate = candidate;
}



@Override
public String toString() {
    return "PhaseItems [id=" + id + ", PhaseItem=" + PhaseItem + ", template=" + template + "]";
}

这是第二类:

@Entity
@Table(name= "candidats")
public class Candidate {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "candidate_id")
    private long id;
    
    private String fullname;
    
    private String username;
    
    @Column(nullable = false, unique = true, length = 45)
    private String email;
    
    private String adress;
    
    private String phoneNumber;
    
    private String password;
    
    @Column(name = "status")
    private String status;
    
    
      @OneToOne(fetch = FetchType.LAZY,
                cascade =  CascadeType.ALL,
                mappedBy = "candidate")
      private PhaseItems phases;
    
    public Candidate(){
        
    }

    public Candidate(String fullname,String username,String email, String adress, String phoneNumber, String password,
            List<JobApplication> appliedJobs) {
        super();
        this.fullname = fullname;
        this.username = username;
        this.email = email;
        this.adress = adress;
        this.phoneNumber = phoneNumber;
        this.password = password;
        this.appliedJobs = appliedJobs;
    }
    
    
    

    public Candidate(String fullname,String username, String email, String adress, String phoneNumber, String password) {
        super();
        this.fullname = fullname;
        this.username = username;
        this.email = email;
        this.adress = adress;
        this.phoneNumber = phoneNumber;
        this.password = password;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

     

    
    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<JobApplication> getAppliedJobs() {
        return appliedJobs;
    }

    public void setAppliedJobs(List<JobApplication> appliedJobs) {
        this.appliedJobs = appliedJobs;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

I have a one to one relation between two classes. I want to create phase items without having to insert candidate Id with it, because I get candidates afterwards so they basically don't exist.

Right now I'm getting the error:

could not execute statement; SQL [n/a]; constraint [null];

because i'm not sending candidateId with it.

This is the first class :

public class PhaseItems {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "phaseI_id")
private long id;

private String PhaseItem;

@ManyToMany(fetch = FetchType.LAZY,
        mappedBy = "items")
@JsonIgnore
private List<PhaseTemplate> template = new ArrayList<>();

 @OneToOne(fetch = FetchType.LAZY, optional = true)
 @JoinColumn(name = "candidate_id", nullable = true)
 private Candidate candidate;
 
 public PhaseItems() {
    super();
 }  



public PhaseItems(long id, String phaseItem) {
    super();
    this.id = id;
    PhaseItem = phaseItem;
}



public long getId() {
    return id;
}


public void setId(long id) {
    this.id = id;
}


public String getPhaseItem() {
    return PhaseItem;
}


public void setPhaseItem(String phaseItem) {
    PhaseItem = phaseItem;
}







public List<PhaseTemplate> getTemplate() {
    return template;
}



public void setTemplate(List<PhaseTemplate> template) {
    this.template = template;
}



public Candidate getCandidate() {
    return candidate;
}



public void setCandidate(Candidate candidate) {
    this.candidate = candidate;
}



@Override
public String toString() {
    return "PhaseItems [id=" + id + ", PhaseItem=" + PhaseItem + ", template=" + template + "]";
}

This is the second class:

@Entity
@Table(name= "candidats")
public class Candidate {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "candidate_id")
    private long id;
    
    private String fullname;
    
    private String username;
    
    @Column(nullable = false, unique = true, length = 45)
    private String email;
    
    private String adress;
    
    private String phoneNumber;
    
    private String password;
    
    @Column(name = "status")
    private String status;
    
    
      @OneToOne(fetch = FetchType.LAZY,
                cascade =  CascadeType.ALL,
                mappedBy = "candidate")
      private PhaseItems phases;
    
    public Candidate(){
        
    }

    public Candidate(String fullname,String username,String email, String adress, String phoneNumber, String password,
            List<JobApplication> appliedJobs) {
        super();
        this.fullname = fullname;
        this.username = username;
        this.email = email;
        this.adress = adress;
        this.phoneNumber = phoneNumber;
        this.password = password;
        this.appliedJobs = appliedJobs;
    }
    
    
    

    public Candidate(String fullname,String username, String email, String adress, String phoneNumber, String password) {
        super();
        this.fullname = fullname;
        this.username = username;
        this.email = email;
        this.adress = adress;
        this.phoneNumber = phoneNumber;
        this.password = password;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

     

    
    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<JobApplication> getAppliedJobs() {
        return appliedJobs;
    }

    public void setAppliedJobs(List<JobApplication> appliedJobs) {
        this.appliedJobs = appliedJobs;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2025-02-11 04:52:39

设置列cantidate_id在您的数据库中为无效。 java属性nullable = 的true @joincolumn注释被忽略。

Set column candidate_id as nullable in your database. Java attribute nullable = true of @JoinColumn annotation is ignored.

寻找一个思念的角度 2025-02-11 04:52:39

是您的列«&nbsp; candidate_id&nbsp;»从表apeas_items在您的模式中无效?

Is your column « candidate_id » from table phase_items nullable in your schema ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文