Spring数据JPA:使用通过`JpaRepository.getOne(id)检索的对象`
在使用JHipster(v6.0.1)Kotlin blueprint(v0.8.0)生成的Spring引导应用程序中,我有以下POST请求处理程序Spring数据JPA:使用通过`JpaRepository.getOne(id)检索的对象`,spring,spring-boot,jpa,spring-data-jpa,jhipster,Spring,Spring Boot,Jpa,Spring Data Jpa,Jhipster,在使用JHipster(v6.0.1)Kotlin blueprint(v0.8.0)生成的Spring引导应用程序中,我有以下POST请求处理程序 @PostMapping("/book") fun createBook(@RequestBody createBookVM: CreateBookVM): ResponseEntity<Book> { val author = authorRepository.getOne(createBookVM.a
@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)
}
@PostMapping(“/book”)
有趣的createBook(@RequestBody createBookVM:createBookVM):ResponseEntity{
val author=authorRepository.getOne(createBookVM.authord)
val userLogin=SecurityUtils.getCurrentUserLogin().OrelsThrow{RuntimeException(“用户未登录”)}
val user=userRepository.findOneByLogin(userLogin).orelsethorn{RuntimeException(“不可能:数据库中不存在用户”)}
val book=book()
book.author=author//FIXME
book.user=用户
log.debug(“Author对象,id:{},Author.id)//这很有效
val结果=bookRepository.save(书本)
返回ResponseEntity.created(URI(“/api/books/”+result.id))
.headers(HeaderUtil.createEntityCreationErt(applicationName,true,ENTITY\u NAME,result.id.toString())
.机构(结果)
}
问题是作者
未添加到书籍
(书籍。作者
将为空
)。但是,我可以访问author
的值,如日志语句所示。将用户
添加到书籍
也可以正常工作
我想问题是
authorRepository.getOne(createBookVM.authorId)
返回一个代理对象,不是Author
的实例,但我不知道如何处理这种情况。而不是使用authorRepository.getOne(createBookVM.binId)
使用authorRepository.findById(createBookVM.binId)
T getOne(ID)
返回引用,而不是实体
/**
* 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);
/**
* 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);
可选的findById(ID)
返回一个实体
/**
* 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);
/**
* 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);
每周<代码>书籍<代码>和<代码>作者<代码>之间有什么关系?请显示代码如果您将业务逻辑提取到服务方法,并使该方法成为事务性的,那么它是否有效?如果OP希望引用
书中现有的作者,那么getOne
实际上是该工作的正确工具他想要的行为与用户的行为相同,在中,user
返回一个实体,而不是一个引用,并且它被正确地分配给book
。问题是getOne
在这种情况下应该可以工作。事实上,这并不意味着代码还有另一个问题。你所建议的只是一种变通方法和一种不好的做法。你能证明我是正确的,因为这是一种不好的做法吗?从数据库加载实体的整个状态(findById
),而你需要将它与另一个实体链接的只是它的id
(getOne
)