I was creating a simple Employee Management System using Spring Boot. I used H2 database.
I am getting the following error:
EmployeemanagerApplicationTests > contextLoads() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:180
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:795
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:795
Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:178
Caused by: java.lang.IllegalArgumentException at JpaMetamodelImpl.java:194
This is my Employee Manager Application.java
package com.example.employeemanager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories("com.example.employeemanager")
@EntityScan("com.example.employeemanager")
@ComponentScan(basePackages = "com.example.employeemanager")
public class EmployeemanagerApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeemanagerApplication.class, args);
}
}
Here is the Employee.java
package com.example.employeemanager.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import jakarta.persistence.ManyToOne;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
Employee Repository.java
package com.example.employeemanager.Repositories;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.employeemanager.Entities.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@SuppressWarnings("null")
@Override
Optional<Employee> findById(Long id);
}
Employee Service.java
package com.example.employeemanager.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.employeemanager.Entities.Department;
import com.example.employeemanager.Entities.Employee;
import com.example.employeemanager.Repositories.EmployeeRepository;
import java.util.List;
@Service
public class EmployeeService {
@Autowired
private final EmployeeRepository employeeRepository;
@Autowired
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
...
}
Please help me figure out what’s the issue here. I have been stuck for hours and no luck.
I have Annotated correctly, and done everything correctly.
Here's the build.gradle file even:
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'javax.persistence:javax.persistence-api:2.2'
runtimeOnly 'com.h2database:h2:2.2.224'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Thank You in advance!
I tried Annotating it correctly, matching the versions and virtually everything that Chat GPT told me to.
user25450700 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.