我一直在用 SpringBoot + Spring Data JPA 编写我的第一个项目。我使用 MySql 作为我的数据库提供者。我对 Spring Boot 和 Hibernate 有点陌生。
我遵循了 http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/ 上的教程对于我的项目。
在 session 工厂之前一切都清楚了。提到的教程不使用 session 工厂;相反,它使用基于 CrudRepository
的事务。但就我而言,因为我还是 Hibernate 的新手,所以我使用 SessionFactory 进行数据库操作。
在我的项目中,DaoImpl 类如下所示。
@Repository
public class ReadDaoImpl implements ReadDao{
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public static void setSessionFactory(SessionFactory sessionFactory) {
TenderReadDaoImpl.sessionFactory = sessionFactory;
}
@Override
public List<Read> getAllReads() {
System.out.println("DEBUG: Inside ReadDaoImpl.getAllReads");
Session session = sessionFactory.openSession(); // I think that the issue comes from here
Transaction tx = null;
..............................
..............................
}
}
我的项目开始在服务器上运行,没有任何问题。但是当我访问 getAll() 方法的相关 url 时,Eclipse 控制台打印出一个空指针异常,引用以下行。
Session session = sessionFactory.openSession();
我想我错过了 sessionFactory
的一些配置。但我不知道它是什么。
这是我的application.properties
文件。它实际上是来自上述链接的同一文件的副本。
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
# Connection url for the database
spring.datasource.url = jdbc:mysql://localhost:3306/readsdb?useSSL=false
# Username and password
spring.datasource.username = root
spring.datasource.password = password
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = create-drop
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
在我的项目中,没有任何配置文件。我是否需要添加一个来创建 sessionFactory
Bean?我遇到了一个叫做 entityManagerFactory
的东西。我是否需要为这个基于 Spring Jpa 的项目配置 sessionfactory
?如果是,怎么做?
目前我完全没有解决方案。指导我完成这件事。如果能指出这样做的详尽方法,那将对我很有帮助。
谢谢...!
最佳答案
以这种方式在您的@Configuration 类中显式配置SessionFactory。从属性文件中删除配置。我使用 Oracle,你可以相应地替换 mysql 属性:
@Configuration
public class myConfiguration {
//datasource bean
@Bean("dataSource")
public DataSource getDataSource() {
DataSourceBuilder b = DataSourceBuilder.create();
b.url("jdbc:oracle:thin:@localhost:1521:xe");
b.driverClassName("oracle.jdbc.driver.OracleDriver");
b.username("system");
b.password("system");
return b.build();
}
//sessionfactory bean
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("ORM.Model");
return builder.buildSessionFactory();
}
//Hibernate properties:
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "create");
return properties;
}
//transaction manager bean
@Bean
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
关于hibernate - 在SpringBoot + Spring data JPA + MySql项目中配置SessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41703110/