第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JPA WrongArgumentException:SQL 字符串不能為 NULL:

JPA WrongArgumentException:SQL 字符串不能為 NULL:

holdtom 2023-10-19 21:39:25
我正在制作 Spring Boot (2.1.9) api,并使用 Postman 將一些 JSON POST 到我的控制器中的函數(shù),該函數(shù)將其轉(zhuǎn)換為實(shí)體(作者)。我的控制器對(duì)象正確接收該對(duì)象及其字段,將其傳遞給我的服務(wù)對(duì)象并將其傳遞給我的數(shù)據(jù)訪問對(duì)象。調(diào)用 CrudRepository save(S實(shí)體) 函數(shù)時(shí)會(huì)出現(xiàn)問題,盡管打印了字段并驗(yàn)證它們不為空,但我收到一個(gè)異常,聲稱我的字段確實(shí)為空。該實(shí)體未添加到數(shù)據(jù)庫中。我嘗試添加 auto_increment,更改生成類型。我嘗試過在 Postman 中發(fā)送 null 的 Id,也嘗試過不在 Postman 中發(fā)送 Id。我不知道如何解決這個(gè)問題,因?yàn)閯h除 @GenerateValue 似乎可行,但并不理想,因?yàn)?save() 會(huì)根據(jù)這篇文章用新數(shù)據(jù)覆蓋具有相同 id 的任何預(yù)先存在的行。(注意:我還沒有完成任何正確的http狀態(tài),我只想讓它在那之前工作)我的控制器:@RestController@RequestMapping(value = "/lms/admin*")public class AdminController{    @Autowired    AdminService admin;    @PostMapping(path = "/author", produces = "application/json", consumes="application/json")    public ResponseEntity<?> createAuthor(@RequestBody Author author)     {        return new ResponseEntity<>(admin.createAuthor(author),HttpStatus.OK);    }    /*other CRUD operations*/}我的服務(wù):@Componentpublic class AdminService{    @Autowired    private AuthorDataAccess authorDao;    public Author createAuthor(Author author)    {        System.out.println(author.toString());        return authorDao.save(author);    }    /*other service functions*/}我的作者數(shù)據(jù)訪問@Componentpublic interface AuthorDataAccess extends CrudRepository<Author, Integer>{}我的作者實(shí)體:@Entity @Table(name = "tbl_author", schema = "library")public class Author implements Serializable{    private static final long serialVersionUID = 3002288345129007776L;    @Id    @GeneratedValue(strategy = GenerationType.SEQUENCE)    @Column(updatable = false)    private Integer authorId;    private String authorName;    public Author(){}    public Author(String authorName)    {        this.authorName = authorName;    }    public Author(Integer authorId, String authorName)    {        this.authorId = authorId;        this.authorName = authorName;    }    /*hashcode, equals, toString, getters, setters*/}mySQL 表:CREATE TABLE IF NOT EXISTS `library`.`tbl_author` (  `authorId` INT(11) NOT NULL,  `authorName` VARCHAR(45) NOT NULL,  PRIMARY KEY (`authorId`))ENGINE = InnoDBDEFAULT CHARACTER SET = latin1;
查看完整描述

1 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

在我開始創(chuàng)建 api 之前數(shù)據(jù)庫就已經(jīng)存在

為了strategy = GenerationType.SEQUENCE工作,必須存在可用于列的數(shù)據(jù)庫序列authorId。另外,您需要使用@SequenceGenerator注釋指定其名稱,以便 JPA 知道如何找到它:

@GeneratedValue(generator = "authorIdGenerator", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "authorIdGenerator", sequenceName = <existing_sequence_name_in_the_db>, allocationSize=<correct_allocation_size_for_the_sequence>)

要獲取創(chuàng)建的實(shí)體的ID,需要再次遍歷表

為了獲取 ID,Hibernate 需要將更改刷新到 DB,因?yàn)槭?DB 生成 ID。如果您使用JpaRepository代替作為CrudRepository基本接口,您將能夠調(diào)用saveAndFlush代替并讀取生成的ID。


查看完整回答
反對(duì) 回復(fù) 2023-10-19
  • 1 回答
  • 0 關(guān)注
  • 177 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)