首页 > 解决方案 > MySQL 错误:列“位置”不能为空

问题描述

我用谷歌搜索,但我无法找到答案。我从 Eclipse 的控制台收到以下错误。我也发布了我的表格以及 DAO、服务和控制器配置。如果有人可以看到我的错误是什么,请帮助我理解它。谢谢

2020-06-17 09:26:44.859 ERROR 69808 --- [nio-8080-exec-1] c.p.servers.app.utility.LoggingAspect    : could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement

Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'ip_address' cannot be null

MySQL中的表实现

CREATE TABLE server_tb(
server_id INT(11) NOT NULL AUTO_INCREMENT,
ip_address VARCHAR(15) NOT NULL,
os_details VARCHAR(7) NOT NULL,
location VARCHAR(10) NOT NULL,
PRIMARY KEY(server_id)
);




INSERT INTO server_tb(ip_address,os_details,location) VALUES
 ('12.12.89.23','Windows','Palo Alto'),
 ('23.1.23.19','Linux','Palo Alto'),
 ('23.83.48.12','Windows','Texas'),
 ('89.1.2.3','Linux','New Jersey'),
 ('10.2.3.12','Windows','Texas'),
 ('255.255.255.255','Linux','New Jersey'),
 ('231.21.12.38','Linux','New Jersey'),
 ('21.82.231.29','Windows','Palo Alto'),
 ('49.21.34.89','Linux','Texas'),
 ('1.3.89.2','Windows','Palo Alto'),
 ('29.245.21.28','Linux','New Jersey'),
 ('23.123.89.21','Windows','Texas'),
 ('23.89.47.123','Linux','Palo Alto'),
 ('34.21.48.82','Windows','New Jersey'),
 ('84.21.48.23','Linux','Texas'),
 ('112.23.8.2','Windows','New Jersey'),
 ('21.38.29.21','Linux','Palo Alto'),
 ('21.23.42.21','Windows','Texas'),
 ('92.22.48.21','Linux','New Jersey'),
 ('2.32.8.21','Windows','Texas');

来自 DAO

@Override
    public void save(Server server) {
        // save or update the server
        Server dbserver = entityManager.merge(server);


        // update with ip address from server

        server.setIpAddress(dbserver.getIpAddress());

    }

从服务

@Override
    public void save(Server server) throws Exception {
        serverDAO.save(server);

    }

从 RestController

@PostMapping("/save")
    public String saveServer(@ModelAttribute("server") Server server) throws Exception {
        try {
            serverService.save(server);

            return "redirect:/servers/findall";
        } catch (Exception e) {
            throw new Exception("Error");
        }

    }

服务器实体

package com.project.servers.app.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="server_tb")
public class Server {

    // define fields
    @Id
    @Column(name="server_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

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

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

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

    // define constructors

    public Server() {

    }

    public Server(String ipAddress, String osDetails, String location) {
        this.ipAddress = ipAddress;
        this.osDetails = osDetails;
        this.location = location;
    }



    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getOsDetails() {
        return osDetails;
    }

    public void setOsDetails(String osDetails) {
        this.osDetails = osDetails;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Server [ipAddress=" + ipAddress + ", osDetails=" + osDetails + ", location=" + location + "]";
    }



}

更新

我将默认值添加到我的 server_tb 表中,并且不再出现错误,但这些值没有保存在数据库中。所有值都作为 NULL 传递。

标签: mysqleclipsespring-boot

解决方案


推荐阅读