在插入记录数据库期间,我正在努力解决以下问题。
2018-08-12 16:06:26.874警告5482 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1364,SQLState: HY000 2018-08-12 16:06:26.875错误5482 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper:字段'country_id'不 查询的默认值为:插入bi_person(additional_info, 性别ID,姓氏,名称)值(?,?,?,?),参数['Java master',48,'WW','John'] 2018-08-12 16:06:27.047错误5482- [nio-8080-exec-2] o.a.c.c.C。[。[。[。[dispatcherServlet]: 带有路径的上下文中Servlet [dispatcherServlet]的Servlet.service() [/ bi-message-svc / v1]引发异常[请求处理失败; 嵌套的异常是org.springframework.orm.jpa.JpaSystemException: 无法执行语句;嵌套异常为 org.hibernate.exception.GenericJDBCException:无法执行 声明],具有根本原因
org.mariadb.jdbc.internal.util.dao.QueryException:字段“ country_id” 没有默认值查询是:插入bi_person (additional_info,gender_id,last_name,name)值(?,?,?,?), 参数['Java master',48,'WW','John'] *
// coutry:
@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;
答案 0 :(得分:0)
您只需要仔细阅读错误消息即可。它说:
字段“ country_id”没有默认值查询是:插入到bi_person(附加信息,性别ID,姓氏,姓名)...
因此,您的bi_person表具有一个名为country_id
的字段,该字段没有任何默认值。当然,由于它在实体Person中没有任何此类字段,因此Hibernate不会在该字段中插入任何内容。这样你就得到了例外。
数据库架构与您的实体设计不匹配。修复一个或多个,或者两个都固定,并确保它们彼此兼容。
如果您的数据库设计是某人有一个国家(因此本人为country_id列)。在您的对象设计中,一个人有许多个国家/地区。
答案 1 :(得分:0)
感谢您的答复。您能否看一下我的数据库架构?
CREATE TABLE IF NOT EXISTS `bi_person` (
`id_person` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`additional_info` VARCHAR(50) NOT NULL,
`gender_id` INT NOT NULL REFERENCES bi_gender,
`country_id` INT NOT NULL REFERENCES bi_country
) COLLATE='utf8_bin';
-- -----------------------------------------------------
-- Table 'bi`.`gender`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bi_gender` (
`id_gender` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(10) NOT NULL
) COLLATE='utf8_bin';
-- -----------------------------------------------------
-- Table 'bi`.`country`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bi_country` (
`id_country` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(10) NOT NULL
) COLLATE='utf8_bin';