Micronaut:发生意外错误:无法获取当前线程的事务同步会话

问题描述 投票:0回答:1

我使用 micronaut 数据框架遇到了下一个错误

19:53:29.910 [default-nioEventLoopGroup-1-2] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
        at io.micronaut.transaction.hibernate.MicronautSessionContext.currentSession(MicronautSessionContext.java:61)

JPA配置

jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: none

存储库

@Repository
public interface MyRepository extends
    PageableRepository<MyEntity, Long>, JpaSpecificationExecutor<MyEntity> {

}

服务

@Slf4j
@Singleton
@AllArgsConstructor
public class MyServiceImpl implements MyService {

  private final MyRepository repository;

  @Override
  public List<TimeSeriesEntryDto<Long>> getForecast(Specification<MyEntity> spec) {
    final  Optional<MyEntity> forecasts =
        repository.findOne(spec); //Executing this line the error pops up

根据Spring Hibernate - 无法获取当前线程的事务同步会话“您必须将@Transactional添加到您的@Repository中”,可能我缺乏此建议背后的知识,而且我还没有看到任何示例完成了。

micronaut-data
1个回答
0
投票

添加@ReadOnly修复了错误。

导入 io.micronaut.transaction.annotation.ReadOnly;

@ReadOnly
public List<TimeSeriesEntryDto<Long>> getForecast(Specification<MyEntity> spec) {
    final  Optional<MyEntity> forecasts =
        repository.findOne(spec);
© www.soinside.com 2019 - 2024. All rights reserved.