Search code examples
javaspringspring-data-jpaspring-repositories

UnsatisfiedDependencyException of JpaRepositories


I'm writing a jpa repostory example and I'm getting a runtime exception of type UnsatisfiedDependencyException.

Here is my program:

@Configuration
@EnableJpaRepositories(basePackageClasses = { PersonRepository.class, ProfessionRepository.class})
@ComponentScan( basePackageClasses =MyService.class)
public class SpringDataJpa  {
        public static void main( String[] args ) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringDataJpa.class);
        service myService = applicationContext.getBean(MyService.class);
    }
}

The service interface:

public interface service {
    void add( Person person );
    List<Person> getListOfPersons();
}

The implementation that throw the exception:

@Service
public class MyService implements service {

    @Autowired
    PersonRepository personRepository;

    @Override
    public void add( Person person ){
        System.out.println("saved");
    }
    @Override
    public List<Person> getListOfPersons() {
        return  null;
    }
}

The repositories:

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {

}
@Repository
public interface ProfessionRepository extends JpaRepository<Profession, Integer> {

}

The exception i'm getting:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'personRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Post-processing of merged bean definition failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/SynchronizationType

As I checked this discussion, I added the proposed dependencies. My dependencies in the pom.xml file:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>LATEST</version>
    </dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>LATEST</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.197</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

How to solve this error?

My second question is: should we use @EnableJpaRepositories if we use Spring Boot?


Solution

  • a datasource and an entityManagerFactory beans were missing. To solve my problem i added the followed code in my configuration class:

        @Bean
        public DataSource firstDataSource (){
            DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
            driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/ride_tracker?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
            driverManagerDataSource.setUsername("root");
            driverManagerDataSource.setPassword("password");
            return driverManagerDataSource;
        }
    
        @Bean
        LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource) {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(dataSource);
            entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
            entityManagerFactoryBean.setPackagesToScan("data");
            Properties jpaProperties = new Properties();
            jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            entityManagerFactoryBean.setJpaProperties(jpaProperties);
            return entityManagerFactoryBean;
        }