gpt4 book ai didi

java - 数据库持久化过程中的hibernate嵌套异常

转载 作者:行者123 更新时间:2023-11-29 17:12:35 26 4
gpt4 key购买 nike

我在插入记录数据库期间遇到以下问题。

2018-08-12 16:06:26.874 WARN 5482 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY000 2018-08-12 16:06:26.875 ERROR 5482 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'country_id' doesn't have a default value Query is: insert into bi_person (additional_info, gender_id, last_name, name) values (?, ?, ?, ?), parameters ['Java master',48,'WW','John'] 2018-08-12 16:06:27.047 ERROR 5482 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/bi-message-svc/v1] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause

org.mariadb.jdbc.internal.util.dao.QueryException: Field 'country_id' doesn't have a default value Query is: insert into bi_person (additional_info, gender_id, last_name, name) values (?, ?, ?, ?), parameters ['Java master',48,'WW','John']*

//国家:

@Table(name = "bi_country")
@Entity
public class Country {

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

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

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="country_book", joinColumns=@JoinColumn(name="id_country"), inverseJoinColumns=@JoinColumn(name="id_person"))
private Set<Person> persons;

//性别:

@Table(name = "bi_gender")
@Entity
public class Gender {

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

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

//人

@Table(name = "bi_person")
@Entity
public class Person {

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

@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "additional_info")
private String additionalInfo;


@ManyToOne
private Gender gender;

@ManyToMany(cascade=CascadeType.ALL)
private Set<Country> countries;

最佳答案

您只需仔细阅读错误消息即可。它说:

Field 'country_id' doesn't have a default value Query is: insert into bi_person (additional_info, gender_id, last_name, name) ...

因此,您的 bi_person 表有一个名为 country_id 的字段,该字段没有任何默认值。当然,Hibernate 不会在该字段中插入任​​何内容,因为它在实体 Person 中没有任何此类字段。所以你会得到这个异常(exception)。

数据库架构与您的实体设计不匹配。修复其中一个或另一个或两者,并确保它们彼此兼容。

如果您的数据库设计,一个人有一个国家/地区(因此列country_id为person)。在你的对象设计中,一个人有许多国家。

关于java - 数据库持久化过程中的hibernate嵌套异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51809641/

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