Configuring embedded Derby in Spring Boot app

只愿长相守 提交于 2020-08-07 04:41:57

问题


Could you please help me set up connection to the embedded Derby database in Spring Boot application?

I searched the web but can only find solutions for server-type Derby, not for embedded Derby.

spring.jpa.database = ?
spring.jpa.hibernate.ddl-auto = create-drop

回答1:


Derby as an in-memory database

If you want to Configure in-memory Derby database with spring boot, the minimal thing you need is to mention the runtime dependency (along with spring-boot-starter-data-jpa) as

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <scope>runtime</scope>
</dependency>

This bare minimum configuration should work for any embedded datasource in Spring Boot. But as of current Spring Boot version (2.0.4.RELEASE) this lead to an error only for Derby

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing
DDL via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist

This happens because of default configuration ofspring.jpa.hibernate.ddl-auto=create-drop see this spring-boot issue for details.

So you need to override that property in your application.properties as

spring.jpa.hibernate.ddl-auto=update

Derby as a persistent database

If you want to use Derby as your persistent database. Add these application properties

spring.datasource.url=jdbc:derby:mydb;create=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.hibernate.ddl-auto=update



回答2:


You don't need connection properties if you are using Spring Boot. Just add these to your POM:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
    </dependency>

Then add the usual controller, service, and repository classes.



来源:https://stackoverflow.com/questions/44411950/configuring-embedded-derby-in-spring-boot-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!