Issue
I have a very simple Springboot JPA Project consisting of a main class a repository and one entity.
First I have had a problem the Springboot throws an error during startup, but this is solved.
Now I face the problem that I cannot retrieve the Repository Bean in my main class.
It throws
"No qualifying bean of type ‘com.d043114.minimalJPA.CityRepository’ available"
The repository looks like this
package com.d043114.minimalJPA;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CityRepository extends CrudRepository<City, Long> {
}
The entity is like this:
package com.d043114.minimalJPA;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "cities")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO )
private long ID;
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
The main class is basic as well
package com.d043114.minimalJPA;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.d043114.minimalJPA.CityRepository;
@EnableJpaRepositories("com.d043114.minimalJPA.*")
@ComponentScan(basePackages = "com.d043114.minimalJPA.*")
@SpringBootApplication
@EntityScan("com.d043114.minimlaJPA")
public class MinimalJpaApplication {
public static void main(String[] args) {
ConfigurableApplicationContext appcontext = SpringApplication.run(MinimalJpaApplication.class, args);
CityRepository cityRepository = appcontext.getBean(CityRepository.class );
}
}
Solution
The issue is with this line
@EnableJpaRepositories("com.d043114.minimalJPA.*")
I replaced this with
@EnableJpaRepositories("com.d043114.minimalJPA")
and removed this line
@EntityScan("com.d043114.minimlaJPA")
It is working .
Also if remove all together these lines then also it is working .
@EnableJpaRepositories("com.d043114.minimalJPA.*")
@ComponentScan(basePackages = "com.d043114.minimalJPA.*")
So final code is
CityRepository.java
package com.d043114.minimalJPA;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CityRepository extends CrudRepository<City, Long>
{
}
City.java
package com.d043114.minimalJPA;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "cities")
public class City
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long ID;
public long getID()
{
return ID;
}
public void setID(long iD)
{
ID = iD;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
private String name;
}
Main class MinimalJpaApplication.java
@SpringBootApplication
public class MinimalJpaApplication
{
public static void main(String[] args)
{
ConfigurableApplicationContext appcontext = SpringApplication.run(MinimalJpaApplication.class, args);
CityRepository cityRepository = appcontext.getBean(CityRepository.class );
if ( cityRepository != null )
{
System.out.println("We are goood ");
}
}
}
If you expand @SpringBootApplication , you will see its already taking care of all these things .
Regarding @EntityScan and @ComponentScan , please see below link
https://www.baeldung.com/spring-entityscan-vs-componentscan
"We should be aware that using @EntityScan will disable Spring Boot auto-configuration scanning for entities."
Adding pom.xml
<?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>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.d043114</groupId>
<artifactId>minimalJPA </artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>minimalJPA </name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Answered By – Knowledge_seeker
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0