Spring Boot最新版本Embedded Derby无法使用,报Schema 'SA'不存在错误
Hey there, let's break down why your new Spring Boot project (generated via Spring Initializr) is throwing that frustrating Schema 'SA' does not exist error, even though your older Derby setup works perfectly. This is usually a mismatch in default configurations between your old project and the newer Spring Boot version—here are the most reliable fixes:
1. Force Derby to Create the Database & Schema Automatically
The core issue is often that your new project isn't telling Derby to create the in-memory database (and its default SA schema) on startup. Add these configurations to your application.properties (or YAML equivalent):
For application.properties:
# Configure embedded Derby with auto-create spring.datasource.url=jdbc:derby:memory:your-db-name;create=true spring.datasource.username=SA spring.datasource.password= spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
For application.yml:
spring: datasource: url: jdbc:derby:memory:your-db-name;create=true username: SA password: "" driver-class-name: org.apache.derby.jdbc.EmbeddedDriver
The ;create=true flag is critical here—it tells Derby to spin up the database (including the default SA schema) if it doesn't already exist.
2. Adjust Hibernate's DDL Auto Strategy
Newer Spring Boot versions might default to a stricter Hibernate DDL setting that tries to validate an existing schema instead of creating it. Update your JPA configuration to let Hibernate manage schema creation:
# Use 'update' to preserve data between restarts, or 'create' for fresh schemas each time spring.jpa.hibernate.ddl-auto=update
update: Updates existing schema and creates missing tables/columns (great for development)create: Drops and recreates the schema on every startup (useful for testing)create-drop: Same ascreate, but drops the schema when the app shuts down
3. Align Spring Boot & Derby Version Compatibility
If your old project uses Spring Boot 2.x and the new one uses 3.x, note that Spring Boot 3.x uses Jakarta EE instead of Java EE. Double-check your Derby dependency is correctly scoped in your build file:
Maven (pom.xml):
<dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <scope>runtime</scope> </dependency>
Gradle (build.gradle):
runtimeOnly 'org.apache.derby:derby'
Spring Initializr should handle this automatically, but it's worth verifying to avoid version conflicts.
4. Explicitly Set the Default Schema (Last Resort)
If the above fixes don't work, explicitly tell Hibernate to use the SA schema:
spring.jpa.properties.hibernate.default_schema=SA
You can also annotate your entity classes with @Table(schema = "SA"), but this is less ideal than fixing the root configuration issue.
Quick Cross-Check Tip
Compare your old project's application.properties/application.yml with the new one—chances are your old project had one of these configurations that the new Initializr-generated project is missing.
内容的提问来源于stack exchange,提问作者Preethi Rajaraman

