将Spring Boot添加到现有的hibernate - 嵌入式derby应用程序中

时间:2017-11-14 15:00:03

标签: spring hibernate derby

我已经有一个现有的hibernate和嵌入式德比工作应用程序。 我的hibernate.cfg.xml具有以下设置

<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:data;create=true</property>
<property name="connection.username">application</property>
<property name="connection.password">password</property>

<property name="hibernate.default_schema">my_application</property>

一切都工作正常,但我希望为该项目添加spring boot,我正在

  

无法创建sessionFactory   object.org.hibernate.service.spi.ServiceException:无法创建   请求服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

当我从hibernate加载sessionFactory时。为什么我不这样做?从Spring Boot加载嵌入式Derby是唯一的方法吗?我不希望spring boot与数据库有任何关系。

此外,添加spring boot后,即使我根本没有运行spring boot,也不再读取log4j.properties文件。

1 个答案:

答案 0 :(得分:0)

找到它。需要像这样设置application.properties

  

spring.jpa.show -sql = true spring.jpa.hibernate.ddl-auto = update   spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.DerbyDialect

     

spring.datasource.url = JDBC:德比:数据;创建=真   spring.datasource.username =应用   spring.datasource.password =密码   spring.datasource.driver类名= org.apache.derby.jdbc.EmbeddedDriver

     

spring.datasource.initialize =真

     

spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext

我的启动应用程序需要像这样的hibernate bean

@SpringBootApplication
@EntityScan("core.peristence.dtos")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }

}

我的服务现在可以像这样自动装配sessionFactory:

@Service
public class MyService {

  @Autowired
  private SessionFactory sessionFactory;

...

}