【问题标题】:In MicronautProject, using hibernate specifications throw exception: Could not obtain transaction-synchronized Session for current thread在 MicronautProject 中,使用休眠规范会抛出异常:Could not gain transaction-synchronized Session for current thread
【发布时间】:2020-04-03 03:56:42
【问题描述】:

我正在研究 micronaut。当我在 micronaut 项目中使用休眠时。一切正常。我在 docs.micronaut.io 学习。但是我使用规范。抛出异常:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:143)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:473)
    at io.micronaut.data.hibernate.operations.HibernateJpaOperations.getCurrentEntityManager(HibernateJpaOperations.java:620)
    at io.micronaut.data.spring.jpa.intercept.FindAllSpecificationInterceptor.intercept(FindAllSpecificationInterceptor.java:49)
    at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:80)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
    at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:123)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
    at jpatest.repository.BookRepository$Intercepted.findAll(Unknown Source)

控制器

@Controller("/book")
public class BookController {
    @Inject
    BookService bookService;
    @Transactional
    @Get()
    HttpResponse book() {
        return bookService.findAll();
    }
}

服务


@Singleton
public class BookService {
    @Inject
    BookRepository bookRepository;
    public HttpResponse findAll() {

        Specification<Book> bookSpecification = (Specification<Book>) (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("id"),2);
        List all = bookRepository.findAll(bookSpecification);
//        Iterable<Book> all = bookRepository.findAll();
        return HttpResponse.ok(all);
    }
}

存储库

@Repository
@Transactional
public interface BookRepository extends CrudRepository<Book, Long>, JpaSpecificationExecutor<Book> {
    Book find(String title);
}

实体

@Entity
@Table(name = "book")
public class Book implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private int pages;

    public Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    public Book() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }
}

应用程序.xml

jpa:
  default:
    entity-scan:
      packages: 'jpatest.entity'

当我使用 @Transactional 时它不起作用。我确定使用

import org.springframework.transaction.annotation.Transactional;

也许我需要配置一下

【问题讨论】:

  • 问题提到了Application.xml,但随后显示了 YAML 代码。是 src/main/resources/application.yml 中的 YAML 代码吗?

标签: java hibernate micronaut-data


【解决方案1】:

除非您在 Micronaut 服务中使用 micronaut-data-spring,否则不应将 org.springframework.transaction.annotation.Transactional 与 Micronaut Data 一起使用,在您的情况下,您不必使用任何 @Transactional 注释(Spring 或其他)。您的 BookRepository 应该自动成为事务性的。

【讨论】:

【解决方案2】:

我错过了 micronaut-spring 依赖项。如果我导入它,它将起作用。

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多