Spring 数据 JPA:使用通过“JpaRepository.getOne(id)”检索的对象
Spring Data JPA: using object retrieved with `JpaRepository.getOne(id)`
在我的 Spring 使用 JHipster (v6.0.1) Kotlin 蓝图 (v0.8.0) 生成的引导应用程序中,我有以下 POST 请求处理程序
@PostMapping("/book")
fun createBook(@RequestBody createBookVM: CreateBookVM): ResponseEntity<Book> {
val author = authorRepository.getOne(createBookVM.authorId)
val userLogin = SecurityUtils.getCurrentUserLogin().orElseThrow { RuntimeException("User not logged in") }
val user = userRepository.findOneByLogin(userLogin).orElseThrow { RuntimeException("IMPOSSIBLE: user does not exist in DB") }
val book= Book()
book.author = author // FIXME
book.user = user
log.debug("Author object with id : {}", author.id) // THIS WORKS
val result = bookRepository.save(book)
return ResponseEntity.created(URI("/api/books/" + result.id))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()))
.body(result)
}
问题是 author
没有添加到 book
(book.author
将是 null
)。但是,我可以访问 author
的值,如日志语句所示。将 user
添加到 book
也可以正常工作。
我想问题是 authorRepository.getOne(createBookVM.authorId)
returns 一个代理对象,而不是 Author
的实例,但我不知道如何处理这种情况。
而不是使用 authorRepository.getOne(createBookVM.binId)
使用 authorRepository.findById(createBookVM.binId)
.
T getOne(ID id)
returns 引用,不是实体。
/**
* Returns a reference to the entity with the given identifier.
*
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object)
* @throws javax.persistence.EntityNotFoundException if no entity exists for given {@code id}.
*/
T getOne(ID id);
Optional<T> findById(ID id)
returns 一个实体。
/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal Optional#empty()} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}.
*/
Optional<T> findById(ID id);
您也可以使用 authorRepository.findOne(createBookVM.binId)
用于比 jpa 2.x.x 更旧的版本:
/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal null} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}
*/
T findOne(ID id);
在我的 Spring 使用 JHipster (v6.0.1) Kotlin 蓝图 (v0.8.0) 生成的引导应用程序中,我有以下 POST 请求处理程序
@PostMapping("/book")
fun createBook(@RequestBody createBookVM: CreateBookVM): ResponseEntity<Book> {
val author = authorRepository.getOne(createBookVM.authorId)
val userLogin = SecurityUtils.getCurrentUserLogin().orElseThrow { RuntimeException("User not logged in") }
val user = userRepository.findOneByLogin(userLogin).orElseThrow { RuntimeException("IMPOSSIBLE: user does not exist in DB") }
val book= Book()
book.author = author // FIXME
book.user = user
log.debug("Author object with id : {}", author.id) // THIS WORKS
val result = bookRepository.save(book)
return ResponseEntity.created(URI("/api/books/" + result.id))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()))
.body(result)
}
问题是 author
没有添加到 book
(book.author
将是 null
)。但是,我可以访问 author
的值,如日志语句所示。将 user
添加到 book
也可以正常工作。
我想问题是 authorRepository.getOne(createBookVM.authorId)
returns 一个代理对象,而不是 Author
的实例,但我不知道如何处理这种情况。
而不是使用 authorRepository.getOne(createBookVM.binId)
使用 authorRepository.findById(createBookVM.binId)
.
T getOne(ID id)
returns 引用,不是实体。
/**
* Returns a reference to the entity with the given identifier.
*
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object)
* @throws javax.persistence.EntityNotFoundException if no entity exists for given {@code id}.
*/
T getOne(ID id);
Optional<T> findById(ID id)
returns 一个实体。
/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal Optional#empty()} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}.
*/
Optional<T> findById(ID id);
您也可以使用 authorRepository.findOne(createBookVM.binId)
用于比 jpa 2.x.x 更旧的版本:
/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal null} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}
*/
T findOne(ID id);