0

I am trying to create Entities for a Postgres Database using Spring Data JPA and am getting this error the whole time: Error creating bean with name 'rawDataService': Unsatisfied dependency expressed through field 'rawDataRepository':
Error creating bean with name 'rawDataRepository' defined in com.example.testcontroller.repository.RawDataRepository defined in @EnableJpaRepositories declared on ApplicationConfig: Not a managed type: class com.example.testcontroller.entity.RawData

PlainJpaConfig:


import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;




@Configuration

@Import(InfrastructureConfig.class)
@ComponentScan(basePackages = "com.*")
@EntityScan(basePackages = "com.*")

public class PlainJpaConfig {

}

InfraStructureConfig:

import javax.sql.DataSource;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
@EnableTransactionManagement
public class InfrastructureConfig {


    @Bean
    public DataSource dataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.postgresql.Driver");
        dataSourceBuilder.url("jdbc:postgresql://localhost:5460/greensoftdb");
        dataSourceBuilder.username("user");
        dataSourceBuilder.password("pwd");
        return dataSourceBuilder.build();
    }


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.POSTGRESQL);
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.*");
        factory.setDataSource(dataSource());

        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return txManager;
    }
}

ApplicationConfig:


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@Import({InfrastructureConfig.class, PlainJpaConfig.class})
@EnableJpaRepositories(basePackages = "com.*")

public class ApplicationConfig {

}

RawData:

package com.example.testcontroller.entity;

import javax.persistence.*;

@Entity
@Table(name="RawData")
public class RawData {
    //same as in MqttDataModel
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    public Long id;
    //TODO:mqttID?
    //Same
    public double timestamp;
    //Same
    public double energy_value;
    //Same
    public String topics;
}

RawDataRepository:

package com.example.testcontroller.repository;



import com.example.testcontroller.entity.RawData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RawDataRepository extends JpaRepository<RawData, Long> {

}
package com.example.testcontroller.repository;



import com.example.testcontroller.entity.RawData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RawDataRepository extends JpaRepository<RawData, Long> {

}

RawDataService:

package com.example.testcontroller.service;

import com.example.testcontroller.entity.RawData;
import com.example.testcontroller.repository.RawDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RawDataService {
    @Autowired
    private RawDataRepository rawDataRepository;

    public List<RawData> findAll(){
        return rawDataRepository.findAll();
    }




}

TestcontrollerApplication:

package com.example.testcontroller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication


public class TestcontrollerApplication {

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

}

Pom.xml:

4.0.0 org.springframework.boot spring-boot-starter-parent 3.0.0 com.example testcontroller 0.0.1-SNAPSHOT testcontroller testcontroller <java.version>17</java.version> org.springframework.boot spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20220924</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.30</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.6.14.Final</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>


</dependencies>

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

I have EntityScan,EnableJPARepositories and everything else in config files, so I am really lost. I can't find what is wrong. Thank you for your help!

Liko
  • 3
  • 4
  • why are you configuring your project, It should be taken care of by spring boot automatically, I am confused – providerZ Dec 04 '22 at 20:38
  • Because I got Error creating bean with name 'entityManagerFactory' before I added configurations – Liko Dec 04 '22 at 20:40
  • What is the purpose of InfrastructureConfig, ApplicationConfig, PlainJpaConfig? In Spring Boot if you add Spring Data JPA starter dependency, all of that is autoconfigured – artild Dec 04 '22 at 20:44
  • I tried commenting out the configurations, but I am still getting the same error. I have added the dependency – Liko Dec 04 '22 at 20:47
  • If I delete the configurations, I still get: "Error creating bean with name 'rawDataRepository' defined in com.example.testcontroller.repository.RawDataRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.testcontroller.entity.RawData" – Liko Dec 04 '22 at 20:49
  • I thought that if I add configurations maybe it would solve it, because it was suggested in other questions with the same error message – Liko Dec 04 '22 at 20:50
  • do you have hibernate-core dependency? and also spring-boot-starter-jpa ? if you have both then remove the first one , hibernate is included in spring-boot-starter-jpa it is causing a conflict if you have both – providerZ Dec 04 '22 at 20:52
  • Yes, I had both. I removed it, but the error is still there. I added my pom.xml to the post. Thank you so much for your help! I really don't understand what have I don wrong – Liko Dec 04 '22 at 20:54
  • also, remove this hibernate-entitymanager dependency – providerZ Dec 04 '22 at 21:01
  • I have done it, but it is still there. I also added spring.datasource.url=jdbc:postgresql://localhost:5460/greensoftdb spring.datasource.username=user spring.datasource.password=pwd spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect to application.properties – Liko Dec 04 '22 at 21:09
  • you didn't have that before, well I am even more shocked, is it working now? – providerZ Dec 04 '22 at 21:10
  • I had it in Config DataSource method and no it is still not working. And I had it before I added config in properties , but it still wasn't working – Liko Dec 04 '22 at 21:15
  • That is not very helpful. I know that the user and password are right, because I can connect to the database using those – Liko Dec 04 '22 at 21:39
  • Try this one, i see that you're also using java 17 (https://stackoverflow.com/a/74787520/6700091) – Kamil Bęben Jan 23 '23 at 18:50

0 Answers0