简体   繁体   中英

Micronauts Failed to inject value for parameter [entityManager] of class: UserRepository

I'm trying to inject EntityManager in the repository. compilation was successful but when i run the application and send a post request i got this error:

Unexpected error occurred: Failed to inject value for parameter [entityManager] of class: aututor.apps.DAO.Repository.RepositoryImp.UserRepository

Message: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new UserController([UserService service]) --> new UserServiceImpl([IUserRepository userRepository]) --> new UserRepository([EntityManager entityManager])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [entityManager] of class: aututor.apps.DAO.Repository.RepositoryImp.UserRepository

this is the repository :

package aututor.apps.DAO.Repository.IRepository;

import aututor.apps.DAO.Model.*;

public interface  IUserRepository  {
    public User save(User User);
    public  User findByEmail(String Email);
}

package aututor.apps.DAO.Repository.RepositoryImp;

import aututor.apps.DAO.Model.User;
import aututor.apps.DAO.Repository.IRepository.IUserRepository;
import io.micronaut.configuration.hibernate.jpa.scope.CurrentSession;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Singleton
public class UserRepository implements IUserRepository {

    @PersistenceContext
    protected  EntityManager entityManager;

    public UserRepository(@CurrentSession EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public User save(User User) {
        return  entityManager.merge(User);
    }


    @Override
    public User findByEmail(String Email) {
        return entityManager.createQuery("SELECT user FROM User user  WHERE user.Email = :email", User.class)
                .setParameter("email", Email)
                .getSingleResult();
    }
}

this is the service :

package aututor.apps.Services.ServicesImpl;

import aututor.apps.DAO.Model.User;
import aututor.apps.DAO.Repository.IRepository.IUserRepository;
import aututor.apps.DTO.Mapper.UserMapper;
import aututor.apps.DTO.UserDTO;
import aututor.apps.Services.IServices.UserService;
import aututor.apps.Util.Response;

import javax.inject.Singleton;

@Singleton
public class UserServiceImpl  implements UserService {
    protected final IUserRepository userRepository;
    protected final UserMapper userMapper= new UserMapper();

    public UserServiceImpl(IUserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public Response CreateUser(UserDTO User) {

        if(userRepository.findByEmail(User.getEmail())==null) {
            User u = userRepository.save(userMapper.ToUser(User));
            return new Response("request has succeeded",userMapper.ToUserDTO(u),200);
        }
        else {
            return new Response("Not Acceptable : Adress email already exist",null,406);
        }
    }

    @Override
    public Response UpdateUser(UserDTO User) {
        return null;
    }

    @Override
    public Response DeleteUser(UserDTO User) {
        return null;
    }

    @Override
    public Response FindUserByID(Long Id) {
        return null;
    }

}

Controller :

package aututor.apps.Controllers;

import aututor.apps.DTO.UserDTO;
import aututor.apps.Services.IServices.UserService;
import aututor.apps.Util.Response;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/rest/User")
public class UserController {
protected final UserService service ;

    public UserController(UserService service) {
        this.service = service;
    }

    @Post("/")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response CreateUser(@Body  UserDTO user) {
        return service.CreateUser(user);
    }
}

build.gradle :

dependencies {
    annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    annotationProcessor "io.micronaut:micronaut-inject-java"
    annotationProcessor "io.micronaut:micronaut-validation"
    annotationProcessor "io.micronaut.data:micronaut-data-processor:1.0.0"
    implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
    implementation "io.micronaut:micronaut-security-jwt"
    implementation "io.micronaut:micronaut-http-client"
    implementation "io.micronaut:micronaut-validation"
    implementation "io.micronaut:micronaut-http-server-netty"
    implementation "io.micronaut.configuration:micronaut-hibernate-jpa"
    implementation("io.micronaut:micronaut-spring")
    compile "io.micronaut.configuration:micronaut-jdbc-tomcat"
    compile "io.micronaut:micronaut-runtime"
    compile "io.micronaut:micronaut-inject"
    compile("io.micronaut.data:micronaut-data-hibernate-jpa")
    compile("io.micronaut.data:micronaut-data-jdbc")
    compile("org.postgresql:postgresql:42.2.1")
    runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
    testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    testAnnotationProcessor "io.micronaut:micronaut-inject-java"
    testImplementation platform("io.micronaut:micronaut-bom:$micronautVersion")
    testImplementation "org.junit.jupiter:junit-jupiter-api"
    testImplementation "io.micronaut.test:micronaut-test-junit5"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}

application.yml:

---
micronaut:
  application:
    name: apps
  server:
    port: 8089

---

datasources:
  default:
    url: jdbc:postgresql://localhost:5432/db
    username: root
    password: ''
    schema-generate: CREATE_DROP
jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.PostgreSQL95Dialect
    entity-scan:
      packages: 'apps.model'

i have tried to work with CrudRepository and there was a problem there also. Everything looks correct i don't know if i miss something.

I started working with Micronaut JPA and Hibernate and came across the same kind of issue that you have. Whatever scans the application.yml is not good at all in telling you if you didn't define your jpa settings properly. After getting experience from beating my head over for days trying to get Micronaut to work with data (and on the brink of giving up on using Micronaut from accessing data at all) from looking at your setup, my best bet is you defined your entity-scan packages wrong. If you specify an entity scan package it must be a package that encapsulates or is a parent encapsulating package of your classes that are annotated with @Entity. In your case you specified apps.model as your package, but I see no where in the code you provided where you are referencing anything that follows that package format. Not sure why you have a aututor.apps.DAO.Model.User package. Model should be separate from your DAO. The easiest thing would be to just delete entity-scan packages and let all your classes get scanned. Specifying the package is supposed to improve performance, but if you don't have a lot of files I don't think it really makes a difference. You could have specified aututor or aututor.apps or aututor.apps.DAO or aututor.apps.DAO.Model as your scan package and that should work if your classes with @Entity are in aututor.apps.DAO.Model package.

在您的存储库类上应用注释@Repository

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM