我正在研究微航。当我在 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;
也许我需要配置某事