Spring Boot - Handle to Hibernate SessionFactory
You can accomplish this with:
SessionFactory sessionFactory =
entityManagerFactory.unwrap(SessionFactory.class);
where entityManagerFactory is an JPA EntityManagerFactory
.
package net.andreaskluth.hibernatesample;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SomeService {
private SessionFactory hibernateFactory;
@Autowired
public SomeService(EntityManagerFactory factory) {
if(factory.unwrap(SessionFactory.class) == null){
throw new NullPointerException("factory is not a hibernate factory");
}
this.hibernateFactory = factory.unwrap(SessionFactory.class);
}
}
How to autowire Hibernate SessionFactory in Spring boot
Try enabling HibernateJpaSessionFactoryBean
in your Spring configuration.
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
Have a look at:
https://stackoverflow.com/a/33881946/676731
By Spring configuration I mean a class annotated with @Configuration
annotation or @SpringBootApplication
(it is implicitly annotated with @Configuration
).
Spring boot - Unable to build hibernate SessionFactory; nested exception is java.lang.UnsupportedOperationException
Spring Boot version 1.5.15.RELEASE
has dependency of Hibernate is <hibernate.version>5.0.12.Final</hibernate.version>
.
So removing <hibernate.version>5.2.13.Final</hibernate.version>
from your pom and let Spring boot download compatible version with it should solve the problem.
after this clean install dependencies and generate war.
and also remove version of mysql connector(<version>5.1.8</version>
):
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
required a bean of type 'org.hibernate.SessionFactory' that not found
Add this to your config file:
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory entityManagerFactory){
return entityManagerFactory.getSessionFactory();
}
Then, in your application.properties file, add
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
Hope it helps!
Related Topics
Curly Braces in "New" Expression? (E.G. "New Myclass() { ... }")
Java.Lang.Noclassdeffounderror: Com.Google.Ads.Adview
Urlconnection Getcontentlength() Is Returning a Negative Value
Efficiency of Searching Using Wherearraycontains
Why Does Bitmapfactory.Decodebytearray Return Null
How to Update One Field from All Documents Using Pojo in Firestore
I Cant See Any Firebaserecycleradapter Items on My Layout
Connectiontimeout Versus Sockettimeout
What Is the Simplest Way to Reverse an Arraylist
Android: How to Hide Actionbar on Certain Activities
Why Does Changing the Sum Order Returns a Different Result
Firebase Database Points to Wrong Database Url
Install Xlsx and Rjava on MACos Mavericks 10.9.5
Java Hotspot(Tm) 64-Bit Server Vm Warning: Ignoring Option Maxpermsize