2023-09-09

H2 Spring Boot - not creating all tables

I have Spring Boot 3.2 with H2 for development. H2 is creating some tables but not all.

I was initially developing my project with MySQL database - but then created a new profile for development in Spring Boot and changed the database to H2 in mem.

It was at this point that some entities do not get tables created in the H2 database.

For example:

HealthCheck

@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Data
@JsonIgnoreProperties({"hibernateLazyInitalizer", "handler"})
@SequenceGenerator(name = "health_check_seq", sequenceName = "HEALTH_CHECK_SEQ", initialValue = 100, allocationSize = 50)
@Table(name = "health_check")
public class HealthCheck {

    @Id
    @GeneratedValue(strategy = SEQUENCE, generator = "health_check_seq")
    private Long id;

    @JsonManagedReference
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Bird bird;
   
    @JsonManagedReference
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "healthCheck")
    private List<Task> tasks;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private LocalDateTime catchDateTime;


    @JsonBackReference
    private Point<G2D> location;

    @ManyToOne
    @JoinColumn(name = "holder_id")
    private User holder;

Application.properties

spring.datasource.url=jdbc:h2:file:~/IdeaProjects/test/h2mem/data;MV_STORE=false;AUTO_SERVER=TRUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.data-locations=classpath:/datadev.sql

Updated app.props:

spring.datasource.url=jdbc:h2:mem:testdb;INIT=CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA_NAME
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

I have tried inserting data in the datadev.sql but it fails because the HEALTH_CHECK table does not exist.

https://pastebin.com/fWgLG6fj

Do I need to add a line to application.properties to tell it not to execute dev.sql until after DDL is finished?

How can I get H2 to create tables for all entities?



No comments:

Post a Comment