Hibernate SQL Error: NULL not allowed for column ‘ID’ on INSERT

I am encountering an issue with Hibernate when trying to insert a record into my database. Here are the details of the problem:

Problem Description: I’m using Hibernate with Spring Boot to persist data to an H2 database. When attempting to save a new record into the ‘device’ table, Hibernate throws an error stating “NULL not allowed for column ‘ID'”. I have defined the ‘ID’ column as a primary key with auto-increment in the database schema.

Database Schema:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><changeSet id="0003-1" author="mwietecki">
<createTable tableName="device">
<column name="id" type="BIGINT">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="manufacturer" type="VARCHAR(255)"/>
<column name="model" type="VARCHAR(255)"/>
<column name="ram" type="INT"/>
<column name="rom" type="INT"/>
<column name="color" type="VARCHAR(50)"/>
<column name="spec" type="VARCHAR(255)"/>
<column name="quantity" type="INT"/>
</createTable>
</changeSet>
</code>
<code><changeSet id="0003-1" author="mwietecki"> <createTable tableName="device"> <column name="id" type="BIGINT"> <constraints primaryKey="true" nullable="false"/> </column> <column name="manufacturer" type="VARCHAR(255)"/> <column name="model" type="VARCHAR(255)"/> <column name="ram" type="INT"/> <column name="rom" type="INT"/> <column name="color" type="VARCHAR(50)"/> <column name="spec" type="VARCHAR(255)"/> <column name="quantity" type="INT"/> </createTable> </changeSet> </code>
<changeSet id="0003-1" author="mwietecki">
    <createTable tableName="device">
        <column name="id" type="BIGINT">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="manufacturer" type="VARCHAR(255)"/>
        <column name="model" type="VARCHAR(255)"/>
        <column name="ram" type="INT"/>
        <column name="rom" type="INT"/>
        <column name="color" type="VARCHAR(50)"/>
        <column name="spec" type="VARCHAR(255)"/>
        <column name="quantity" type="INT"/>
    </createTable>
</changeSet>

Entity class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "device")
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String manufacturer;
private String model;
private int ram;
private int rom;
private String color;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "VARCHAR(255)")
private Spec spec;
private int quantity;
}
</code>
<code>@Entity @Data @NoArgsConstructor @AllArgsConstructor @Table(name = "device") public class Device { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String manufacturer; private String model; private int ram; private int rom; private String color; @Enumerated(EnumType.STRING) @Column(columnDefinition = "VARCHAR(255)") private Spec spec; private int quantity; } </code>
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "device")
public class Device {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String manufacturer;
    private String model;
    private int ram;
    private int rom;
    private String color;

    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "VARCHAR(255)")
    private Spec spec;

    private int quantity;
}

Dto class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Data
@AllArgsConstructor
@NoArgsConstructor
public class DeviceDto {
private Long id;
private String manufacturer;
private String model;
private int ram;
private int rom;
private String color;
private String spec;
private int quantity;
}
</code>
<code>@Data @AllArgsConstructor @NoArgsConstructor public class DeviceDto { private Long id; private String manufacturer; private String model; private int ram; private int rom; private String color; private String spec; private int quantity; } </code>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DeviceDto {
    private Long id;
    private String manufacturer;
    private String model;
    private int ram;
    private int rom;
    private String color;
    private String spec;
    private int quantity;
}

DeviceMapper:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Mapper
public interface DeviceMapper {
DeviceMapper INSTANCE = Mappers.getMapper(DeviceMapper.class);
@Mapping(target = "spec", source = "spec", qualifiedByName = "specToString")
DeviceDto deviceToDeviceDto(Device device);
@Mapping(target = "spec", source = "spec", qualifiedByName = "stringToSpec")
@Mapping(target = "id", ignore = true)
Device deviceDtoToDevice(DeviceDto deviceDto);
@Named("specToString")
default String mapSpecToString(Spec spec) {
return spec != null ? spec.name() : null;
}
@Named("stringToSpec")
default Spec mapStringToSpec(String spec) {
return spec != null ? Spec.valueOf(spec) : null;
}
}
</code>
<code>@Mapper public interface DeviceMapper { DeviceMapper INSTANCE = Mappers.getMapper(DeviceMapper.class); @Mapping(target = "spec", source = "spec", qualifiedByName = "specToString") DeviceDto deviceToDeviceDto(Device device); @Mapping(target = "spec", source = "spec", qualifiedByName = "stringToSpec") @Mapping(target = "id", ignore = true) Device deviceDtoToDevice(DeviceDto deviceDto); @Named("specToString") default String mapSpecToString(Spec spec) { return spec != null ? spec.name() : null; } @Named("stringToSpec") default Spec mapStringToSpec(String spec) { return spec != null ? Spec.valueOf(spec) : null; } } </code>
@Mapper
public interface DeviceMapper {
    DeviceMapper INSTANCE = Mappers.getMapper(DeviceMapper.class);

    @Mapping(target = "spec", source = "spec", qualifiedByName = "specToString")
    DeviceDto deviceToDeviceDto(Device device);

    @Mapping(target = "spec", source = "spec", qualifiedByName = "stringToSpec")
    @Mapping(target = "id", ignore = true)
    Device deviceDtoToDevice(DeviceDto deviceDto);

    @Named("specToString")
    default String mapSpecToString(Spec spec) {
        return spec != null ? spec.name() : null;
    }

    @Named("stringToSpec")
    default Spec mapStringToSpec(String spec) {
        return spec != null ? Spec.valueOf(spec) : null;
    }
}

Method addDevice in Service and Controller class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public DeviceDto addDevice(DeviceDto deviceDto) {
Device device = DeviceMapper.INSTANCE.deviceDtoToDevice(deviceDto);
Device savedDevice = deviceRepository.save(device);
return DeviceMapper.INSTANCE.deviceToDeviceDto(savedDevice);
}
@PostMapping("/add")
@Operation(summary = "Add device")
public DeviceDto addDevice(@RequestBody DeviceDto deviceDto) {
return deviceService.addDevice(deviceDto);
}
</code>
<code> public DeviceDto addDevice(DeviceDto deviceDto) { Device device = DeviceMapper.INSTANCE.deviceDtoToDevice(deviceDto); Device savedDevice = deviceRepository.save(device); return DeviceMapper.INSTANCE.deviceToDeviceDto(savedDevice); } @PostMapping("/add") @Operation(summary = "Add device") public DeviceDto addDevice(@RequestBody DeviceDto deviceDto) { return deviceService.addDevice(deviceDto); } </code>
    public DeviceDto addDevice(DeviceDto deviceDto) {
        Device device = DeviceMapper.INSTANCE.deviceDtoToDevice(deviceDto);
        Device savedDevice = deviceRepository.save(device);
        return DeviceMapper.INSTANCE.deviceToDeviceDto(savedDevice);
    }

@PostMapping("/add")
    @Operation(summary = "Add device")
    public DeviceDto addDevice(@RequestBody DeviceDto deviceDto) {
        return deviceService.addDevice(deviceDto);
    }

I tried to delete ID in DTO class, adding adnotations to Mapper to ignore ID. Also in swagger ui in the request body give the id value as null or any integer

New contributor

m.wietecki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

You are missing autoIncrement="true" in the liquibase changeSet.createTable.column so the DB expects a value. Try:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <column name="id" type="BIGINT" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
</code>
<code> <column name="id" type="BIGINT" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> </code>
    <column name="id" type="BIGINT" autoIncrement="true">
        <constraints primaryKey="true" nullable="false"/>
    </column>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật