MicronautProject中,使用hibernate规范抛出异常:Could not obtain transaction-synchronized Session for current thread
In MicronautProject, using hibernate specifications throw exception: Could not obtain transaction-synchronized Session for current thread
我正在研究微航。当我在 micronaut 中使用休眠时 project.Everything 是 OK.I 在 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;
}
}
Application.xml
jpa:
default:
entity-scan:
packages: 'jpatest.entity'
当我使用@Transactional 时,它 work.I 肯定不会使用
import org.springframework.transaction.annotation.Transactional;
也许我需要配置……
除非您在 Micronaut 服务中使用 micronaut-data-spring
,否则不应将 org.springframework.transaction.annotation.Transactional
与 Micronaut 数据一起使用,在您的情况下,您不必使用任何 @Transactional
注释( Spring 或其他)。您的 BookRepository
应该会自动成为事务性的。
我缺少 micronaut-spring 依赖项。如果我导入它,它将起作用。
我正在研究微航。当我在 micronaut 中使用休眠时 project.Everything 是 OK.I 在 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;
}
}
Application.xml
jpa:
default:
entity-scan:
packages: 'jpatest.entity'
当我使用@Transactional 时,它 work.I 肯定不会使用
import org.springframework.transaction.annotation.Transactional;
也许我需要配置……
除非您在 Micronaut 服务中使用 micronaut-data-spring
,否则不应将 org.springframework.transaction.annotation.Transactional
与 Micronaut 数据一起使用,在您的情况下,您不必使用任何 @Transactional
注释( Spring 或其他)。您的 BookRepository
应该会自动成为事务性的。
我缺少 micronaut-spring 依赖项。如果我导入它,它将起作用。