gpt4 book ai didi

java - 当 Spring/Hibernate JPARepository 返回时,实体处于什么生命周期状态?

转载 作者:行者123 更新时间:2023-11-30 06:03:29 24 4
gpt4 key购买 nike

如果我有一个存储库:

public interface ThingRepository extends JpaRepository<Thing, UUID> {
@Query(/* query to get some Things */)
Collection<Thing> getSomeThings(/* some arguments */);
}

由 Spring/Hibernate Autowiring ,返回的 Thing 实体对象将处于什么状态(持久/分离/ transient /等)?

上下文 - 如果我们对返回的 Thing 进行更改(例如 thing.setThingString("stuff!")),是否存在这些更改会发生的情况无需显式调用 thingRepository.save(thing);?

即可持久保存回数据库

最佳答案

简单地看一下 SimpleJpaRepository:

https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

这是将为 ThingRepository 实现的具体类的基础。

你会看到:

@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>

这意味着每个方法都在只读事务中执行,除非使用写事务进行注释,就像在 save* 方法中所做的那样:

@Transactional
public <S extends T> S save(S entity)

因此,在您的情况下,实体在您的情况下是只读的。

另请阅读 Spring Data JPA 文档:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions

5.7. Transactionality

By default, CRUD methods on repository instances are transactional. For read operations, the transaction configuration readOnly flag is set to true. All others are configured with a plain @Transactional so that default transaction configuration applies. For details, see JavaDoc of SimpleJpaRepository. If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface

关于java - 当 Spring/Hibernate JPARepository 返回时,实体处于什么生命周期状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51821046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com