Pet*_*ter 51 java spring hibernate spring-boot
有谁知道如何处理由Spring Boot创建的Hibernate SessionFactory?
And*_*eas 55
你可以用:
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
其中entityManagerFactory是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);
}
}
Run Code Online (Sandbox Code Playgroud)
ygl*_*odt 48
自动装配Hibernate SessionFactory最简单,最简单的方法是:
这是Spring Boot with Hibernate 4的解决方案:
application.properties:
spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)
配置类:
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
Run Code Online (Sandbox Code Playgroud)
然后您可以SessionFactory像往常一样自动装入您的服务:
@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)
从Spring Boot 1.5到Hibernate 5,现在这是首选方式:
application.properties:
spring.jpa.properties.hibernate.current_session_context_class=
org.springframework.orm.hibernate5.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)
配置类:
@EnableAutoConfiguration
...
...
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
fact.setEntityManagerFactory(emf);
return fact;
}
Run Code Online (Sandbox Code Playgroud)
Han*_*kCa 15
伟大的工作Andreas.我创建了一个bean版本,因此SessionFactory可以自动装配.
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
....
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public SessionFactory getSessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
Run Code Online (Sandbox Code Playgroud)
小智 10
它适用于 Spring Boot 2.1.0 和 Hibernate 5
@PersistenceContext
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)
然后你可以使用 entityManager.unwrap(Session.class) 创建新的 Session
Session session = null;
if (entityManager == null
|| (session = entityManager.unwrap(Session.class)) == null) {
throw new NullPointerException();
}
Run Code Online (Sandbox Code Playgroud)
示例创建查询:
session.createQuery("FROM Student");
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:db11g
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
Run Code Online (Sandbox Code Playgroud)
另一种类似于yglodt的方式
在application.properties中:
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)
在您的配置类中:
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
return hemf.getSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)
然后您可以像往常一样在服务中自动装配SessionFactory:
@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81569 次 |
| 最近记录: |