Home > Enterprise >  Why will Spring Data JDBC not work for me?
Why will Spring Data JDBC not work for me?

Time:03-24

I should probably mention that Spring is my first framework that I am learning for Java. What I am trying to do is to get the JDBC working for me using the Spring Data JDBC dependency I downloaded from spring.io. (2.6.4) I followed a tutorial on YouTube where the guy demonstrated it with something similar to the following code. (Should also mention the code he used was for Spring version 2.2 but I'm not sure where else to find right code for 2.6.4)

There is also an UnsatisfiedDependencyException I keep getting where it says:

Error creating bean with name 'applicationRunner' defined in com.example.demo.DemoApplication: Unsatisfied dependency expressed through method 'applicationRunner' parameter 0;

I do not know why this is happening. But here is the actual code:

package com.example.demo;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    ApplicationRunner applicationRunner(StudentRepo studentRepo) {
        return args -> {
            var s1 = Student.createStudent("John", "Doe");
            var s2 = Student.createStudent("Jane", "Doe");

            System.out.println(studentRepo.save(s1));
            System.out.println(studentRepo.save(s2));

            System.out.println(studentRepo.findByFName("John"));
        };
    }

}

CRUD Repository interface:


import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface StudentRepo extends CrudRepository<Student, String> {

    @Query("SELECT * FROM Students WHERE name = :name")
    List<Student> findByFName(@Param("name") String fName);
}

schema.sql file:

CREATE TABLE Students (
    id VARCHAR(50) IDENTITY PRIMARY KEY,
    firstName VARCHAR(50),
    lastName VARCHAR(50),
    rank VARCHAR(50)
);

I think it's also important for me to include pom.xml file: (Though it should also be mentioned that the pom.xml file didn't originally include the hsqldb dependency. I added that in myself.

<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>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Edit: Exception after including @Repository in the CRUD repository interface:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationRunner' defined in com.example.demo.DemoApplication: Unsatisfied dependency expressed through method 'applicationRunner' parameter 0

Exception after replacing @Repository with @NoRepositoryBean:

2022-03-23 18:30:06.531  INFO 98567 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-03-23 18:30:06.556 ERROR 98567 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

APPLICATION FAILED TO START

Description:

Parameter 0 of method applicationRunner in com.example.demo.DemoApplication required a bean of type 'com.example.demo.StudentRepo' that could not be found.


Action:

Consider defining a bean of type 'com.example.demo.StudentRepo' in your configuration.


Process finished with exit code 1

CodePudding user response:

try modifying the bean:

@Bean

public CommandLineRunner demo(StudentRepo studentRepo) {
   return (args) -> {
   studentRepo.save(new Student("Jack", "Bauer"));
   studentRepo.save(new Student("Chloe", "O'Brian"));

}

you can consult the guide https://spring.io/guides/gs/accessing-data-jpa/

CodePudding user response:

The problem lies in the schema.sql script. An IDENTITY column in HSQLDB has to be of the type INTEGER or BIGINT (see documentation at http://www.hsqldb.org/doc/1.8/guide/ch09.html, section CREATE TABLE). All other errors are just subsequent errors.

  • Related