Environment:
- Java 21
- Spring 7.0.0-M8
- Hibernate 7.1.3
- Example Project: https://github.com/hantsy/spring7-sandbox/blob/master/hibernate-jakarta-data
Description
In Spring 7, Hibernate StatelessSession participate into the transaction which fills the blank when integrating Jakarta Data/Hibernate and Spring.
To test it, I have updated my Jakarta Data example to use Spring 7/Hibernate 7.1 stack.
@jakarta.data.repository.Repository
public interface JakartaDataPostRepository {
@Transactional
@Delete
int deleteAll();
@Transactional
@Save
Post save(Post post);
@Find
List<Post> findAll();
@Query("from Post p where p.title like :s and p.status=:status")
List<Post> findByKeyword(@Param("s") String s, @Param("status") Status status, Limit limit);
@Find
Optional<Post> findById(UUID id);
}
When compiling the project, it generated the implelementation class like this.
@Component
public class JakartaDataPostRepository_ implements JakartaDataPostRepository {
/**
* @see #findByKeyword(String,Status,Limit)
**/
static final String FIND_BY_KEYWORD_String_Status = "from Post p where p.title like :s and p.status=:status";
protected ObjectProvider<StatelessSession> session;
public JakartaDataPostRepository_(ObjectProvider<StatelessSession> session) {
this.session = session;
}
public StatelessSession session() {
return session.getObject();
}
@Override
public Post save(Post post) {
// ...
When running the tests, I got the exception:
Caused by: org.hibernate.HibernateException: Could not obtain
transaction-synchronized Session for current thread
As normal, I have configured Hibernate LocalSessionFactoryBean
and HibernateTransactionManager
in the JakartaDataConfig.