Home > Back-end >  Spring Configuration Problem: Not a managed type: Entity
Spring Configuration Problem: Not a managed type: Entity

Time:12-07

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!

CodePudding user response:

I solved it. After rebuilding the project it could not detect javax anymore and only suggested Jakarta.persistence. Now it is working

  • Related