MySQL ошибка: столбец "местоположение" не может быть нулевым - PullRequest
0 голосов
/ 17 июня 2020

Я погуглил и не смог найти на это ответа. Я получаю указанную ниже ошибку с консоли в 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');

From 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());

    }

From Service

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

    }

From 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...