admin管理员组

文章数量:1304808

I am working on a small project with Spring and i am using the maven profile to set up differents environnements for my spring project. I have set a test, a prod and a dev profile with a H2 database. I want to be able to connect to my persisted H2 database when I am on a dev profile in my spring project. I already have some entities with their relations (author, book, user, reservation, genres, status...) When I try to connect to the H2 console after launching my spring server with:

mvn spring-boot:run -Pdev

I cannot find my tables and also I couldn't connect to my database in the first place without creating myself the dev.mv.db file. All my tables are inside the memory database with a random id, when I look at the log statement, I always see:

o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a'

It seems that spring is ignoring my dev profile and is not creating the database inside the file.

Here are my application-dev.yml:

spring:
    config:
        activate:
            on-profile: dev
    datasource:
        url: jdbc:h2:file:~/h2db/dev;MODE=MySQL
        generate-unique-name: false
        username: sa
        password: password
        driverClassName: .h2.Driver
    sql:
        init:
            mode: never
    jpa:
        database-platform: .hibernate.dialect.H2Dialect
        defer-datasource-initialization: true
        hibernate:
            ddl-auto: create-drop  # Ensure Hibernate creates the tables
    h2:
        console:
            enabled: true
            path: /h2-console
            settings:
                trace: false
                web-allow-others: false

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
    xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0-M3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.billykid</groupId>
    <artifactId>template</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>template</name>
    <description>A template for a fullstack Java React application </description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-testcontainers</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.testcontainers</groupId>
            <artifactId>mysql</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>.springframework.boot</groupId>
                    <artifactId>spring-boot-docker-compose</artifactId>
                    <optional>true</optional>
                </dependency>
                <dependency>
                    <groupId>com.mysql</groupId>
                    <artifactId>mysql-connector-j</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>install-frontend-dependencies</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <workingDirectory>src/main/client</workingDirectory>
                                    <executable>npm</executable>
                                    <arguments>
                                        <argument>install</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>build-and-include-frontend</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <workingDirectory>src/main/client</workingDirectory>
                                    <executable>npm</executable>
                                    <arguments>
                                        <argument>run</argument>
                                        <argument>build</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <property>
                    <name>spring.profiles.active</name>
                    <value>test</value>
                </property>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <dependencies>
                <dependency>
                    <groupId>.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>
                                    <groupId>.projectlombok</groupId>
                                    <artifactId>lombok</artifactId>
                                </exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>;/url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>;/url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

Here is one of my entities:

package com.billykid.template.entity;

import java.time.Instant;
import java.util.List;
import java.util.Set;

import .springframework.data.annotation.CreatedDate;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@Table(name="book")
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;

    @ManyToMany(mappedBy="bookList")
    private Set<Reservation> reservationList;

    @Column(name="title")
    private String title;

    @Column(name="description")
    private String description;

    @Column(name="book_cover")
    private String bookCoverUrl;

    @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
    @CollectionTable(name="genres", joinColumns = @JoinColumn(name="genres_id"))
    @Column(name="genres", nullable = false)
    private List<String> genres;
    
    @ManyToOne
    @JoinColumn(name="author",referencedColumnName="id")
    private Author author;

    @CreatedDate
    @Column(name="added_date")
    private Instant addedDate;

    @Column(name="volume_number")
    private Integer volume;
}

And finally here is the log statement:

2025-02-04T00:18:18.578+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : Starting TemplateApplication using Java 21.0.5 with PID 24552 (C:\Users\billy\Documents\GitHub\Vite_React_Spring_Java_Template\target\classes started by billy in C:\Users\billy\Documents\GitHub\Vite_React_Spring_Java_Template)
2025-02-04T00:18:18.583+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : No active profile set, falling back to 1 default profile: "default" 2025-02-04T00:18:18.617+01:00 INFO 24552 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2025-02-04T00:18:18.617+01:00 INFO 24552 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2025-02-04T00:18:19.086+01:00 INFO 24552 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-02-04T00:18:19.133+01:00 INFO 24552 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 40 ms. Found 1 JPA repository interface. 2025-02-04T00:18:19.521+01:00 INFO 24552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2025-02-04T00:18:19.533+01:00 INFO 24552 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-02-04T00:18:19.533+01:00 INFO 24552 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.30] 2025-02-04T00:18:19.569+01:00 INFO 24552 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-02-04T00:18:19.570+01:00 INFO 24552 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 951 ms 2025-02-04T00:18:19.592+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2025-02-04T00:18:19.729+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a user=SA 2025-02-04T00:18:19.730+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2025-02-04T00:18:19.738+01:00 INFO 24552 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a' 2025-02-04T00:18:19.830+01:00 INFO 24552 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2025-02-04T00:18:19.871+01:00 INFO 24552 --- [ restartedMain] .hibernate.Version : HHH000412: Hibernate ORM core version 6.6.1.Final 2025-02-04T00:18:19.896+01:00 INFO 24552 --- [ restartedMain] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2025-02-04T00:18:20.125+01:00 INFO 24552 --- [ restartedMain] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer 2025-02-04T00:18:20.183+01:00 INFO 24552 --- [ restartedMain] .hibernate.orm.connections.pooling : HHH10001005: Database info: Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)'] Database driver: undefined/unknown Database version: 2.3.232 Autocommit mode: undefined/unknown Isolation level: undefined/unknown Minimum pool size: undefined/unknown Maximum pool size: undefined/unknown 2025-02-04T00:18:20.949+01:00 INFO 24552 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2025-02-04T00:18:21.014+01:00 INFO 24552 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2025-02-04T00:18:21.192+01:00 WARN 24552 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2025-02-04T00:18:21.207+01:00 INFO 24552 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html] 2025-02-04T00:18:21.437+01:00 INFO 24552 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2025-02-04T00:18:21.463+01:00 INFO 24552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2025-02-04T00:18:21.469+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : Started TemplateApplication in 3.193 seconds (process running for 3.485) 2025-02-04T00:18:37.444+01:00 INFO 24552 --- [nio-8080-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2025-02-04T00:18:37.445+01:00 INFO 24552 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2025-02-04T00:18:37.447+01:00 INFO 24552 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

Any help would be appreciated, thank you in advance for your response.

I am working on a small project with Spring and i am using the maven profile to set up differents environnements for my spring project. I have set a test, a prod and a dev profile with a H2 database. I want to be able to connect to my persisted H2 database when I am on a dev profile in my spring project. I already have some entities with their relations (author, book, user, reservation, genres, status...) When I try to connect to the H2 console after launching my spring server with:

mvn spring-boot:run -Pdev

I cannot find my tables and also I couldn't connect to my database in the first place without creating myself the dev.mv.db file. All my tables are inside the memory database with a random id, when I look at the log statement, I always see:

o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a'

It seems that spring is ignoring my dev profile and is not creating the database inside the file.

Here are my application-dev.yml:

spring:
    config:
        activate:
            on-profile: dev
    datasource:
        url: jdbc:h2:file:~/h2db/dev;MODE=MySQL
        generate-unique-name: false
        username: sa
        password: password
        driverClassName: .h2.Driver
    sql:
        init:
            mode: never
    jpa:
        database-platform: .hibernate.dialect.H2Dialect
        defer-datasource-initialization: true
        hibernate:
            ddl-auto: create-drop  # Ensure Hibernate creates the tables
    h2:
        console:
            enabled: true
            path: /h2-console
            settings:
                trace: false
                web-allow-others: false

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache./POM/4.0.0" xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache./POM/4.0.0 https://maven.apache./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0-M3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.billykid</groupId>
    <artifactId>template</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>template</name>
    <description>A template for a fullstack Java React application </description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-testcontainers</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.testcontainers</groupId>
            <artifactId>mysql</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>.springframework.boot</groupId>
                    <artifactId>spring-boot-docker-compose</artifactId>
                    <optional>true</optional>
                </dependency>
                <dependency>
                    <groupId>com.mysql</groupId>
                    <artifactId>mysql-connector-j</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>install-frontend-dependencies</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <workingDirectory>src/main/client</workingDirectory>
                                    <executable>npm</executable>
                                    <arguments>
                                        <argument>install</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>build-and-include-frontend</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <workingDirectory>src/main/client</workingDirectory>
                                    <executable>npm</executable>
                                    <arguments>
                                        <argument>run</argument>
                                        <argument>build</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <property>
                    <name>spring.profiles.active</name>
                    <value>test</value>
                </property>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <dependencies>
                <dependency>
                    <groupId>.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>
                                    <groupId>.projectlombok</groupId>
                                    <artifactId>lombok</artifactId>
                                </exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

Here is one of my entities:

package com.billykid.template.entity;

import java.time.Instant;
import java.util.List;
import java.util.Set;

import .springframework.data.annotation.CreatedDate;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@Table(name="book")
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;

    @ManyToMany(mappedBy="bookList")
    private Set<Reservation> reservationList;

    @Column(name="title")
    private String title;

    @Column(name="description")
    private String description;

    @Column(name="book_cover")
    private String bookCoverUrl;

    @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
    @CollectionTable(name="genres", joinColumns = @JoinColumn(name="genres_id"))
    @Column(name="genres", nullable = false)
    private List<String> genres;
    
    @ManyToOne
    @JoinColumn(name="author",referencedColumnName="id")
    private Author author;

    @CreatedDate
    @Column(name="added_date")
    private Instant addedDate;

    @Column(name="volume_number")
    private Integer volume;
}

And finally here is the log statement:

2025-02-04T00:18:18.578+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : Starting TemplateApplication using Java 21.0.5 with PID 24552 (C:\Users\billy\Documents\GitHub\Vite_React_Spring_Java_Template\target\classes started by billy in C:\Users\billy\Documents\GitHub\Vite_React_Spring_Java_Template)
2025-02-04T00:18:18.583+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : No active profile set, falling back to 1 default profile: "default" 2025-02-04T00:18:18.617+01:00 INFO 24552 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2025-02-04T00:18:18.617+01:00 INFO 24552 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2025-02-04T00:18:19.086+01:00 INFO 24552 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-02-04T00:18:19.133+01:00 INFO 24552 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 40 ms. Found 1 JPA repository interface. 2025-02-04T00:18:19.521+01:00 INFO 24552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2025-02-04T00:18:19.533+01:00 INFO 24552 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-02-04T00:18:19.533+01:00 INFO 24552 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.30] 2025-02-04T00:18:19.569+01:00 INFO 24552 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-02-04T00:18:19.570+01:00 INFO 24552 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 951 ms 2025-02-04T00:18:19.592+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2025-02-04T00:18:19.729+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a user=SA 2025-02-04T00:18:19.730+01:00 INFO 24552 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2025-02-04T00:18:19.738+01:00 INFO 24552 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:42222461-e6cf-4a77-9ca5-c321a81eb65a' 2025-02-04T00:18:19.830+01:00 INFO 24552 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2025-02-04T00:18:19.871+01:00 INFO 24552 --- [ restartedMain] .hibernate.Version : HHH000412: Hibernate ORM core version 6.6.1.Final 2025-02-04T00:18:19.896+01:00 INFO 24552 --- [ restartedMain] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2025-02-04T00:18:20.125+01:00 INFO 24552 --- [ restartedMain] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer 2025-02-04T00:18:20.183+01:00 INFO 24552 --- [ restartedMain] .hibernate.orm.connections.pooling : HHH10001005: Database info: Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)'] Database driver: undefined/unknown Database version: 2.3.232 Autocommit mode: undefined/unknown Isolation level: undefined/unknown Minimum pool size: undefined/unknown Maximum pool size: undefined/unknown 2025-02-04T00:18:20.949+01:00 INFO 24552 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2025-02-04T00:18:21.014+01:00 INFO 24552 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2025-02-04T00:18:21.192+01:00 WARN 24552 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2025-02-04T00:18:21.207+01:00 INFO 24552 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html] 2025-02-04T00:18:21.437+01:00 INFO 24552 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2025-02-04T00:18:21.463+01:00 INFO 24552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2025-02-04T00:18:21.469+01:00 INFO 24552 --- [ restartedMain] c.billykid.template.TemplateApplication : Started TemplateApplication in 3.193 seconds (process running for 3.485) 2025-02-04T00:18:37.444+01:00 INFO 24552 --- [nio-8080-exec-6] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2025-02-04T00:18:37.445+01:00 INFO 24552 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2025-02-04T00:18:37.447+01:00 INFO 24552 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

Any help would be appreciated, thank you in advance for your response.

Share Improve this question asked Feb 3 at 23:46 BillydogTheKidBillydogTheKid 1521 silver badge13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

The configuration file looks fine, but the log doesn't load your configuration file. I guess you might have created a file called application-dev.yml, but it's not the default configuration file to load.

  1. You can create a new application.properties or application.yml file and add:
spring.profiles.active=dev

2.Or add the following to the startup command:

--spring.profiles.active=dev

Hope to help you!

Solution (1) You follow this way with Spring Boot YML 's multiple profiles https://docs.spring.io/spring-boot/reference/features/external-config.html#features.external-config.files.profile-specific

You run dev profile , test profile did not work. Use

mvn spring-boot:run -Dspring-boot.run.profiles=dev

Reference https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html

Current latest version https://docs.spring.io/spring-boot/maven-plugin/using.html#using.overriding-command-line

Solution (2) You also try alternative thinking with brand new test profile. See at the bottom of this page https://docs.spring.io/spring-boot/maven-plugin/using.html#using.overriding-command-line

You can try to change

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

to

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

in this brand new test profile (you can try removing all other types of database: MySQL, etc.).

$ mvn spring-boot:run -Dapp.profiles=test

Hello I finally found the solution, it's because of this statement in the logs:

--- [ restartedMain] c.billykid.template.TemplateApplication : No active profile set, falling back to 1 default profile: "default"

I had to understand that maven profile and application.yml profile are two differents things.

In order to launch the application-dev.yml configuration file when I hit the -Pdev on the mvn spring-boot:run -Pdev command, I need to link the maven profile with the application.yml.

First inside the pom.xml file for each profile, I removed any mention of:

<activation>
    <property>
        <name>spring.profiles.active</name>
        <value>...</value>
    </property>
</activation>
<properties>
    <spring.profiles.active>...</spring.profiles.active>
</properties>

and replace it with a custom property called "applicationProfile" with the name of the profile (dev, prod, test) as a value:

<properties>
     <applicationProfile>dev</applicationProfile>
</properties>

Then in the application.yml file, I used the placeholder (@applicationProfile@) to set up the corresponding configuration file:

spring:
  profiles:
    active: @applicationProfile@

本文标签: javaH2 database is not available in my spring appStack Overflow