简体   繁体   English

PSQLException:错误:关系“床”的“idRoom”列中的 null 值违反非空约束

[英]PSQLException: ERROR: null value in column "idRoom" of relation "Bed" violates not-null constraint

i'm working using spring boot, and i'm trying to add beds to database(postgresql) but i'm facing this error我正在使用 spring 启动,我正在尝试将床添加到数据库(postgresql)但我遇到了这个错误

org.postgresql.util.PSQLException: ERROR: null value in column "idRoom" of relation "Bed" violates not-null constraint
  Detail: Failing row contains (34, sxxkskskso, null).

BedRequest床位请求

package com.bezkoder.springjwt.pojo;

public class BedRequest {


    public Long idBed;


    public String bedNumber;

    public long roomId;


    public Long getIdBed() {
        return idBed;
    }

    public void setIdBed(Long idBed) {
        this.idBed = idBed;
    }

    public String getBedNumber() {
        return bedNumber;
    }

    public void setBedNumber(String bedNumber) {
        this.bedNumber = bedNumber;
    }

    public long getRoomId() {
        return roomId;
    }

    public void setRoomId(long roomId) {
        this.roomId = roomId;
    }
}

Bed

package com.bezkoder.springjwt.models;

import javax.persistence.*;

@Entity
public class Bed {




    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long idBed;

    private String bedNumber ;


    @ManyToOne
            (fetch = FetchType.LAZY)
    @JoinColumn(nullable = false, name = "idRoom")
    private Room room;


    public Bed(Long idBed, String bedNumber) {
        this.idBed = idBed;
        this.bedNumber = bedNumber;

    }

    public Bed() {

    }




    public String getBedNumber() {
        return bedNumber;
    }

    public void setBedNumber(String bedNumber) {
        this.bedNumber = bedNumber;
    }
    public Long getIdBed() {
        return idBed;
    }

    public void setIdBed(Long idBed) {
        this.idBed = idBed;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }
}

Room Class房间 Class

  @Override
    public Bed addBed(BedRequest bedRequest) {
        Room room = roomRepository.findByIdRoom(bedRequest.roomId);

        Bed bed = new Bed();

        bed.setBedNumber(bedRequest.bedNumber);
        bed.setRoom(room);
        return bedRepository.save(bed);
    }

BedController class床控制器 class

   @PostMapping("/createBed")
    public Bed AddBed(@RequestBody BedRequest bedRequest){



        return bedService.addBed(bedRequest);
    }

Debug Result调试结果

{
  "timestamp": "2021-05-30T18:01:33.500+0000",
  "status": 400,
  "error": "Bad Request",
  "message": "Required request body is missing: public com.bezkoder.springjwt.models.Bed com.bezkoder.springjwt.controllers.BedController.AddBed(com.bezkoder.springjwt.pojo.BedRequest)",
  "path": "/api/auth/bed/createBed"
}

I really don't understand what is happening underneath here, if someone able to help me with this?我真的不明白这里发生了什么,如果有人能帮我解决这个问题? I've looked in the internet for answers but nothing's worked so far.我在互联网上寻找答案,但到目前为止没有任何效果。

You have error in the mapping, please check your Room class and put this:您在映射中有错误,请检查您的房间 class 并输入:

@OneToMany
        (fetch = FetchType.LAZY, mappedBy = "room")
private Set<Bed> beds;

put this in your serviceImpl you cand add Bed by id:把它放在你的 serviceImpl 你可以通过 id 添加床:

 @Override
    public Bed addBed(BedRequest bedRequest) {
        Room room = roomRepository.findByIdRoom(bedRequest.idRoom);

        Bed bed = new Bed();

        bed.setBedNumber(bedRequest.bedNumber);
        bed.setRoom(room);
        return bedRepository.save(bed);
    }

and this in BedController:这在 BedController 中:

 @PostMapping("/createBed")
    public ResponseEntity<?> AddBed(@RequestBody BedRequest bedRequest){

        bedService.addBed(bedRequest);
        return ResponseEntity.ok(new MessageResponse("Bed registered successfully!"));

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PSQLException:错误:列中的空值违反了非空约束 - PSQLException: ERROR: null value in column violates not-null constraint 突发错误:org.postgresql.util.PSQLException:错误:列“id”中的空值违反非空约束 - Sudden error: org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint org.postgresql.util.PSQLException:错误:“ category_id”列中的空值违反了非空约束 - org.postgresql.util.PSQLException: ERROR: null value in column “category_id” violates not-null constraint org.postgresql.util.PSQLException:错误:列“id”中的空值违反非空约束 - org.postgresql.util.PSQLException: ERROR: null value in column “id” violates not-null constraint Spring 启动后 api - org.postgresql.util.PSQLException:错误:“密码”列中的 null 值违反了非空约束 - Spring boot post api - org.postgresql.util.PSQLException: ERROR: null value in column "password" violates not-null constraint 错误:关系 xxx 的“id”列中的 null 值违反非空约束 - Spring 数据 JPA - ERROR: null value in column "id" of relation xxx violates not-null constraint - Spring Data JPA 由以下原因引起:org.postgresql.util.PSQLException:错误:列“ student_id”中的空值违反了非空约束 - Caused by: org.postgresql.util.PSQLException: ERROR: null value in column “student_id” violates not-null constraint 关系“invoice”的“header_id”列中的 JPA null 值违反 PostgresSQL 的非空约束 - JPA null value in column "header_id" of relation "invoice" violates not-null constraint with PostgresSQL Spring Boot + PostgreSQL: 原因:org.postgresql.util.PSQLException:错误:“front_menu_id”列中的空值违反了非空约束 - Spring Boot + PostgreSQL: Cause: org.postgresql.util.PSQLException: ERROR: null value in column "front_menu_id" violates not-null constraint 如何修复列“id”中的&#39;null值违反了非空约束&#39;&#39; - How to fix ''null value in column “id” violates not-null constraint''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM