gpt4 book ai didi

java - 为什么 JpaRepository 在保存实体时尝试为空 FK 键创建实体?

转载 作者:太空宇宙 更新时间:2023-11-04 09:08:03 26 4
gpt4 key购买 nike

我有一个Person表,其中包含字段country(FK,可以为空)。当我尝试使用空的 国家/地区 值保存个人时,我会收到错误 500 和消息

2020-01-30 15:16:37.277 警告 10524 --- [nio-8098-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL 错误:0,SQLState:235022020-01-30 15:16:37.277 错误 10524 --- [nio-8098-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ОШИБКА: нулевое значение в столбце "code"нарушае т ограничение NOT NULL 答案: Ошибочная строка содержит (42,null,null,null)。2020-01-30 15:16:37.299 错误 10524 --- [nio-8098-exec-1] o.z.problem.spring.common.AdviceTraits:内部服务器错误

org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [不适用];约束[空];嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行语句

粗略错误推导:

Error: null value in "code" column breaks NOT NULL restriction Details: The error string contains (42, null, null, null).

Person.java

@Entity
@Table(name = "t_person")
@Data
public class Person {
@Id
@Column(name = "person_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String surname;
private String name;
private String patronymic;

@Column(name = "surname_rus")
private String surnameRus;

@Column(name = "name_rus")
private String nameRus;

@Column(name = "surname_eng")
private String surnameEng;

@Column(name = "name_eng")
private String nameEng;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
private Country country;

private String settlement;
private String occupation;

@ManyToMany
@JoinTable(
name = "person_link",
joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "person_id"),
inverseJoinColumns = @JoinColumn(name = "link_id", referencedColumnName = "link_id"))
private List<UrlLink> linkList;

@ManyToMany
@JoinTable(
name = "org_actor",
joinColumns = @JoinColumn(name = "actor_id", referencedColumnName = "person_id"),
inverseJoinColumns = @JoinColumn(name = "org_id", referencedColumnName = "org_id"))
private List<Org> orgList;

private String description;

@ManyToMany
@JoinTable(
name = "person_hashtag",
joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "person_id"),
inverseJoinColumns = @JoinColumn(name = "hashtag_id", referencedColumnName = "hashtag_id"))
private List<HashTag> hashtagList;


************
Country.java

@Entity
@Table(name="t_country")
@Data

public class Country {
@Id
@Column(name = "country_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer id;
private String code;
private String name;
}

create table t_country
(
country_id serial not null
constraint t_country_pkey
primary key,
code char(3) not null
constraint unique_constr_code
unique,
name text
constraint unique_constr_name
unique,
miscellany text,
constraint constr_name
unique (code, name)
);

如何保存国家为空的 Person 实体?

最佳答案

在您所在国家/地区的数据库表定义中,字段代码定义为

code
char(3) not null
constraint unique_constr_code
unique,

表示数据库级别存在非空约束。

这就是为什么你无法拯救一个国家/地区为空字段的人。

因此,即使您在 java 代码中没有非空约束,数据库也会拒绝该值。也许您在删除 java 代码中的 not null 约束后忘记迁移数据库?

关于java - 为什么 JpaRepository 在保存实体时尝试为空 FK 键创建实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59987313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com