com.springboot.DatabaseSeeder中构造函数的参数0需要一个名为' entityManagerFactory'的bean。无法找到

时间:2018-04-24 11:48:22

标签: java hibernate spring-boot spring-data-jpa hibernate-entitymanager

我遇到了'entityManagerFactory'的问题。和启动应用程序时的DatabaseSeeder。有人可以帮我解决这个问题吗?这些问题在其他计算机上的lauch项目之后开始

实体

@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
public interface HotelRepository extends JpaRepository<HotelBooking, Long> {
    List<HotelBooking> findByPricePerNightLessThan(double price);
}

控制器

@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>
    <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>

错误讯息: com.springboot.DatabaseSeeder中构造函数的参数0需要一个名为&#39; entityManagerFactory&#39;的bean。无法找到。

    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.

0 个答案:

没有答案