N
N
Nikita K2020-11-19 21:53:34
Spring
Nikita K, 2020-11-19 21:53:34

Why is the jpaRepository bean not being created?

Deploying for the first time. through the idea, everything works fine, but when you try to deploy to vps, it gives an error, the github project
Stackrace errors:

[main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancel        ling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bot' defined in URL [jar:file:/root/Telegr        am.jar!/com/nikita/bot/Bot.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.Unsati        sfiedDependencyException: Error creating bean with name 'channelService' defined in URL [jar:file:/root/Telegram.jar!/com/nikita/service/ChannelService.class]: U        nsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean         with name 'jpaChannelRepository' defined in com.nikita.repository.JpaChannelRepository defined in @EnableJpaRepositories declared on App: Cannot create inner be        an '(inner bean)#3382f8ae' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.        springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3382f8ae': Cannot resolve reference to bean 'entityManagerFactor        y' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory'         available
21:43:19.367 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bot' defined in URL [jar:file:/root/Telegram.jar!/com/nikita/bot        /Bot.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyExcepti        on: Error creating bean with name 'channelService' defined in URL [jar:file:/root/Telegram.jar!/com/nikita/service/ChannelService.class]: Unsatisfied dependency         expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaChannel        Repository' defined in com.nikita.repository.JpaChannelRepository defined in @EnableJpaRepositories declared on App: Cannot create inner bean '(inner bean)#3382f        8ae' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.        factory.BeanCreationException: Error creating bean with name '(inner bean)#3382f8ae': Cannot resolve reference to bean 'entityManagerFactory' while setting const        ructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

Channel Service:
import com.nikita.model.Channel;
import com.nikita.repository.JpaChannelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChannelService {

    private final JpaChannelRepository jpaChannelRepository;

    @Autowired
    public ChannelService(JpaChannelRepository jpaChannelRepository) {
        this.jpaChannelRepository = jpaChannelRepository;
    }


    public List<Channel> findByStart(boolean start){
        return jpaChannelRepository.findAllByStart(start);
    }

    public Channel update(Channel channel){
        return jpaChannelRepository.save(channel);
    }

    public void delete(String id){
        jpaChannelRepository.deleteById(id);
    }

    public List<Channel> findAll(){
        return jpaChannelRepository.findAll();
    }

    public Channel findById(String id){
        return jpaChannelRepository.findById(id).get();
    }
}


JpaChannelRepository:
package com.nikita.repository;

import com.nikita.model.Channel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface JpaChannelRepository extends JpaRepository<Channel, String> {

    @Override
    Optional<Channel> findById(String id);

    @Query("SELECT c FROM Channel c WHERE c.start=:start")
    List<Channel> findAllByStart(@Param("start") boolean start);

    @Override
    <S extends Channel> S save(S s);

    @Override
    void deleteById(String id);


}


I myself tried to understand what the problem was all day, none of the methods found on the Internet helped ..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2020-11-20
@xez

It looks like you are missing the "entityManagerFactory" bean:

No bean named 'entityManagerFactory' available

Try to declare it somewhere.

O
Orkhan Hasanli, 2020-11-23
@azerphoenix

Hello!
Did you manage to solve the problem?
If not, then I will share some thoughts, and you already debug your code.

Deploying for the first time. through the idea, everything works fine, but when you try to deploy to vps, it gives an error

If it works correctly for you locally, but not on the VPS, then check the server configuration. In your github project, application.yaml is empty and, accordingly, it is not clear what exactly is written in your configs for production. If you have profiles, then check if it hibernate.ddl-auto: validateis installed and if it is installed, whether you have imported ddl & dml to the server. Or do you have it in production should create ddl itself?
At the very least, this is the first point that you should pay attention to.
Next, it’s worth clarifying whether you are deploying the application to an external tomcat or are you using embedded?
Next, try adding this class to your project:
public class ServletInitializer extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(App.class);
  }
}

Next, pay attention to how exactly you build build and what you add to it. For example, war exploded or war or jar. Does it have all required classes in it etc.
If the build is via Gradle, then use bootWar. In general, you need to debug and see what's wrong

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question