This guide shows how to handle
schema generation in
Spring Boot with
Apache Derby using JPA and Hibernate configuration options.
1. Add Dependencies
In
pom.xml
:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. Configure Schema Generation
In
application.properties
:
spring.datasource.url=jdbc:derby:memory:demoDB;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Options for
spring.jpa.hibernate.ddl-auto
:
create
→ generates schema each startup
update
→ updates schema without dropping data
create-drop
→ creates schema and drops it when app stops
none
→ no automatic schema changes
3. Create Entity
import jakarta.persistence.*;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
// getters and setters
}
When you run the app, Hibernate generates the
PRODUCT
table in Derby.
4. Repository
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product, Long> {
}
5. REST Controller
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductRepository repo;
public ProductController(ProductRepository repo) {
this.repo = repo;
}
@PostMapping
public Product add(@RequestBody Product p) {
return repo.save(p);
}
@GetMapping
public List<Product> all() {
return repo.findAll();
}
}
6. Test Schema Generation
Run app:
mvn spring-boot:run
The
Product
table is created automatically in Derby.
Test API:
curl -X POST http://localhost:8080/products -H "Content-Type: application/json" -d '{"name":"Book","price":12.5}'
curl http://localhost:8080/products
image quote pre code