Skip to content
Advertisement

Reading data from a sql database with spring data fails

I’m having a database with existing tables. One of these is called AKTIVE_AUFTRAEGE with the id “AKTIVE_AUFTRAGE_ID” and the second field “ROBOTER_AUFTRAG_ID”. I want to read the data using Spring data. I followed some tutorials an my code looks like this:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:sqlserver://****;databaseName=****;schema=dbo
spring.datasource.username=****
spring.datasource.password=****
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

and

package hello;

import javax.persistence.*;

@Entity
@Table(name = "AKTIVE_AUFTRAEGE")
public class AktiveAuftraege {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="AKTIVE_AUFTRAEGE_ID")
    private Integer auftragID;
    private Integer ROBOTER_AUFTRAG_ID;

... getter and setter
    }

and

package hello;


import org.springframework.data.repository.CrudRepository;

public interface AuftraegeRepsository extends CrudRepository<AktiveAuftraege, Integer> {
        AktiveAuftraege findByauftragID(Integer aktive_auftraege_id);
}

and

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class Test {
    @Autowired
    private AuftraegeRepsository auftraegeRepsository;


    public void testAll(){
        if (auftraegeRepsository != null) {
            Iterable<AktiveAuftraege> results = auftraegeRepsository.findAll();
            for (AktiveAuftraege e : results) {
                System.out.println(e);
            }
        } else {
            System.out.println("ISNULL!");
        }
    }
}

and the main

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        Test t = new Test();
        t.testAll();

    }
}

I’m very new to Spring data and have a couple of questions:

  • How can I check if the application has a connection to the database?
  • Why is the autowired repository always null?
  • I assume the problem mentioned problem is caused in the application class with the new operator. How can I avoid this? (Or is this fine?)
  • Do I need to implement all variables of a table in a class or can I just implement the ones I want to get back and igore the other columns?

Advertisement

Answer

How can I check if the application has a connection to the database?

If the application starts up correctly, it means you have a connection to the database

Why is the autowired repository always null?

Because you instantiate the Test class yourself, and Spring is thus out of the equation, and thus doesn’t inject anything in the object you created behind its back, since it doesn’t even know about it.

How can I avoid this?

By getting it from the ApplicationContext returned by SpringApplication.run(), or by creating a bean of type CommandLineRunner and injecting the Test inside it. Check the Spring Boot documentation.

Do I need to implement all variables of a table in a class or can I just implement the ones I want to get back and igore the other columns?

If you just want to read from that table, having only a subset of the columns is fine. But if you want to insert data in it, the other columns will be ignored, and will thus always have their default value (or null if there is no default value).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement