簡體   English   中英

oracle.jdbc.OracleDatabaseException: ORA-01400: 不能插入 NULL

[英]oracle.jdbc.OracleDatabaseException: ORA-01400: cannot insert NULL into

這是我的實體類,當我運行項目時自動映射oracle數據庫。

import lombok.Data;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.time.LocalDateTime;

@Data
@Entity
@Table(name = "test")
public class UserDetail {

    @SequenceGenerator(
            name = "sequence_name",
            allocationSize = 1
    )
    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "sequence_name"
    )
    @Column(name = "info_id", length = 11)
    private int id;

    @Column(name = "phone_number", length = 20)
    private String phoneNumber;

    @Lob
    @Column(name = "text_message")
    private String textMessage;

    @CreatedDate
    @Column(columnDefinition = "TIMESTAMP", nullable = false)
    private LocalDateTime createdDate;

}

我用更高版本的ORACLE做了IDENTITY,還是不行。

這是我的代碼,在這里我用 swagger 發送文件擴展名,如下所示:

C:\Users\anar.memmedov\Desktop\file2.xlsx


 public Response acceptExcellFileAndInsertToDatabase(String file) {
        try (FileInputStream excelFile = new FileInputStream(file)) {
            String phoneNumber = "";
            String textMessage = "";
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();
            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();
                while (cellIterator.hasNext()) {
                    Cell currentCell = cellIterator.next();
                    if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                        phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
                    } else if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        textMessage = String.valueOf(currentCell.getStringCellValue());
                    }

                }
            }
            this.userDetailRepository.save(phoneNumber, textMessage);
            excelFile.close();
            Path sourcePath = Paths.get(file);
            Path targetPath = Paths.get("C:\\Users\\anar.memmedov\\Desktop\\ko\\" + sourcePath.getFileName());
            Files.move(sourcePath, targetPath);

            return new SuccessResponse(MessageCase.FILE_SUCCESSFULLY_WRITTEN_TO_DATABASE.getMessage(), 200);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErrorResponse(MessageCase.FAILED_HAPPEND_WHEN_FILE_WRITTEN_TO_DATABASE, 404);
    }

這是我的存儲庫方法,我使用一個 excel 文件並將其中的值發送到您看到的表中的列,但出現以下錯誤:

@Modifying
@Query(value = "INSERT INTO test (PHONE_NUMBER,TEXT_MESSAGE) VALUES (:phoneNumber,:textMessage)", nativeQuery = true)
@Transactional
void save(@Param("phoneNumber") String phoneNumber, @Param("textMessage") String textMessage);

oracle.jdbc.OracleDatabaseException: ORA-01400: cannot insert NULL into ("TEST3"."TEST"."ID")
INFO_ID 創建日期 電話號碼 TEXT_MESSAGE

我不知道發生了什么,但是當我使用 mysql 數據庫時它起作用了,我的意思是,我正在獲取 excel 文件,將其中的值寫入數據庫,然后成功地將 excel 文件提取到我計算機上的文件夾中。 但是現在我無法解決這個錯誤,因為我正在使用oracle。

錯誤說:

不能將 NULL 插入 ("TEST3"."TEST"." ID ")

你正在插入

INSERT INTO test (PHONE_NUMBER,TEXT_MESSAGE) --> 這里沒有ID

如果它在 MySQL 中有效但在 Oracle 中無效,我認為 MySQL 的ID列是自動生成的,而在 Oracle 中則不是這樣。

那么該怎么辦? 確保ID以某種方式獲取其值。

  • 如果您在 Excel 文件中有它,請使用它
  • 如果不
    • 如果您使用的 Oracle 數據庫版本低於 12c,請創建一個數據庫觸發器,該觸發器將使用序列將值插入ID
    • 如果您使用的是 12c 及更高版本,則可以創建一個自動生成值的標識
  • 如果您不關心 ID(即它可以是NULL ),請刪除該約束。 可能NOT NULL ,但它可能是一個主鍵。 我不認為這實際上這里的一個選擇

我解決了這個問題。 我在實體類的 createdDate 變量中將**true**添加到 nullable 並且它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM