0

I have some problems with 'entityManagerFactory' and DatabaseSeeder when launch app. Could somebody help me to solve this issue? The problems starts after lauch project on other computer

Entity

@Entity
@Table(name = "hotel")
public class HotelBooking {
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="hotel_id")
    @Id
    private Long id;
    private String hotelName;
    private double pricePerNight;
    private int nbOfNights;

    public HotelBooking(String hotelName, double pricePerNight, int nbOfNights) {
        this.hotelName = hotelName;
        this.pricePerNight = pricePerNight;
        this.nbOfNights = nbOfNights;
    }

    public HotelBooking() {
    }

    //getters and setters
}

Repository

@Repository
public interface HotelRepository extends JpaRepository<HotelBooking, Long> {
    List<HotelBooking> findByPricePerNightLessThan(double price);
}

Controller

@RestController
@RequestMapping(value = "/bookings")
public class HotelController {
    HotelRepository hotelRepository;

    @Autowired
    public HotelController(HotelRepository hotelRepository){
        this.hotelRepository = hotelRepository;
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<HotelBooking> getAll(){
        return hotelRepository.findAll();
    }

    @RequestMapping(value = "/affordable/{price}", method = RequestMethod.GET)
    public List<HotelBooking> getAffordable(@PathVariable double price){
        return hotelRepository.findByPricePerNightLessThan(price);
    }

    @RequestMapping(value ="/create", method = RequestMethod.POST)
    public List<HotelBooking> create(@RequestBody HotelBooking hotelBooking){
       hotelRepository.save(hotelBooking);
        return hotelRepository.findAll();
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public List<HotelBooking> remove(@PathVariable Long id){
        hotelRepository.deleteById(id);
        return hotelRepository.findAll();
    }
}

DatabaseSeeder

@Component
public class DatabaseSeeder implements CommandLineRunner {

    private HotelRepository hotelRepository;

    @Autowired
    public DatabaseSeeder(HotelRepository hotelRepository){
        this.hotelRepository = hotelRepository;
    }

    @Override
    public void run(String... strings) throws Exception {
        List<HotelBooking> bookings = new ArrayList<>();
        bookings.add(new HotelBooking("Marriot", 200.5, 3));
        bookings.add(new HotelBooking("Ibis", 300.5, 4));
        bookings.add(new HotelBooking("Novotel", 100.5, 1));
        hotelRepository.saveAll(bookings);
    }
}

pom xml dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.16.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>4.5.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
</dependencies>

Error message : Parameter 0 of constructor in com.springboot.DatabaseSeeder required a bean named 'entityManagerFactory' that could not be found.

    2018-04-24 14:20:14.421  WARN 6996 --- [main] 
    ConfigServletWebServerApplicationContext : Exception encountered during 
    context initialization - cancelling refresh attempt: 
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
    creating bean with name 'databaseSeeder' defined in file 
    [C:\Users\Nazar\Desktop\SiteDemo\target\classes\com\springboot
    \DatabaseSeeder.class]: 
    Unsatisfied dependency expressed through constructor parameter 
    0; nested exception is 
    org.springframework.beans.factory.BeanCreationException: Error creating 
    bean with name 'hotelRepository': Cannot create inner bean '(inner 
    bean)#7048535f' 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)#7048535f': Cannot resolve reference to bean 
    'entityManagerFactory' while setting constructor argument; nested 
    exception is 
     org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
     bean named 'entityManagerFactory' available

    Parameter 0 of constructor in com.springboot.DatabaseSeeder required a 
    bean named 'entityManagerFactory' that could not be found.
veljkost
  • 1,748
  • 21
  • 25
  • Possible duplicate of [Spring-boot: required a bean named 'entityManagerFactory' that could not be found](https://stackoverflow.com/questions/48416927/spring-boot-required-a-bean-named-entitymanagerfactory-that-could-not-be-foun) – RubioRic Apr 24 '18 at 11:58
  • You seem to be using mongodb and jpa/hibernate. Those don't go together, mongodb is a document database instead of a relational database such as MySQL. – semvdwal Apr 24 '18 at 11:59
  • For starters stop mixing hibernate versions.. You are mixing jars of 4 different versions... – M. Deinum Apr 24 '18 at 12:22
  • 1
    (https://stackoverflow.com/questions/6499641/using-both-mongodb-and-mysql-in-one-project) here is a sample of using 2 databases in one project. So it`s not the problem. Sorry about hibernate dependencies, they were commited in project, but i forgot to delete them from code in question. My project worked properly on one computer, but when I tried to launch it on another it brokes. Thanks everybody for help, Ive already solved my issue. [Bane](https://stackoverflow.com/users/483257/bane) I hope you will be more tolerant to other people, even if their questions may be stupid on your opinion. –  Apr 24 '18 at 19:20

0 Answers0