Home>

I want to access DB using JPA in Spring.
An error will occur if this is described in pom.xml in the project.
Finally, I want to run the project with Maven install and send it to BUILD SUCCESS and console.

Error message

JPA settings cannot be made in pom.xml in the project.

Error message


[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] ShopApplicationTests.contextLoads »IllegalState Failed to load ApplicationCon ...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ----------------------------------------------- -------------------------
[INFO] BUILD FAILURE
[INFO] ----------------------------------------------- -------------------------
[INFO] Total time: 9.525 s
[INFO] Finished at: 2019-11-29T15: 13: 49 + 09: 00
[INFO] ----------------------------------------------- -------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins: maven-surefire-plugin: 2.22.2: test (default-test) on project shop: There are test failures.
[ERROR]
[ERROR] Please refer to C: \ Program Files \ pleiades \ workspace \ shop \ target \ surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date] .dump, [date] -jvmRun [N] .dump and [date] .dumpstream.
[ERROR]->[Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Applicable source code
<? xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
    xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent><groupId>org.springframework.boot</groupId>
         spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath /><!-lookup parent from repository->
    </parent>
    <groupId>jp.co.sss.shop</groupId>
     shop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shop</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>13</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                     junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency><!-Settings for using JPA (this causes an error)->
        <dependency>
            <groupId>org.springframework.boot</groupId>
             spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-Settings to use JDBC (object7.jar build path setting)->
        <dependency>
            <groupId>com.oracle.jdbc</groupId>
             ojdbc7</artifactId>
            <version>12.1.0.2</version>
            <scope>system</scope>
            <systemPath>${basedir} /src/main/resources/lib/ojdbc7.jar</systemPath>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                 spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

-If i comment out the corresponding source code, it has become BUILD SUCCESS, so we know that JPA settings are not made
-Add JPA from adding dependencies instead of writing code directly in xml
・ The library (?) Javax.persistence.XX that gives an error if JPA is not set in the Java class was used after writing the corresponding source code, but an error occurs when trying to execute it in the SpringBoot application

Supplemental information (FW/tool version etc.)

Spring (STS) 3.9.10
Java jdk13.0.1
DB Oracle Database 11g Express Edition
Template engine Thymleaf2.2.1
Build tool Maven
Servlet container Tomcat9

Beginning Spring learning.
I'm writing the code exactly as I did in the textbook,
It did n’t work.

  • Answer # 1

    Ifspring-boot-starter-data-jpais added to the dependency, the default is not set in the application configuration file (application.properties or application.yml) I think the test class created in fails.

    In this case, the following three points can be considered as countermeasures.
    Anything is fine, but the simplest is 1) or 2), and 3) is essential if you are writing code that will eventually connect to the database.

    1) Delete or disable the test class created by default.

    The test class created by default doesn't make much sense, so you can delete or disable it.

    Example) Delete

    DeleteShopApplicationTests.javawhere the error occurred.

    Example: Disable

    EditShopApplicationTests.javawhere the error occurred as follows.

    @Disabled
    @SpringBootTest
    class ShopApplicationTests {
    // ... omitted ...
    }
    2) Specify the skip option so that the test is not executed at build time.

    If you don't want to modify the test code, you can skip it at build time.

    Example) From Eclipse

    Open the Maven build execution configuration screen and check "Skip test" surrounded by a border

    Example) From the command line
    >mvn clean package -DskipTests = true
    3) Set the data source in the application setting file.

    Describe at least the necessary data source settings. After this setting, it is necessary to be able to connect to the database at the time of build.

    # JDBC URL of the database.
    spring.datasource.url =
    # Login username of the database.
    spring.datasource.username =
    # Login password of the database.
    spring.datasource.password =