2013-07-01 53 views
2

我正在使用Spring Data JPA來開發Spring MVC應用程序。我構建了一個JPA存儲庫。Spring數據jpa:沒有找到類型爲void的屬性flush

public interface AccessReportRepository extends JpaRepository<AccessReport, Long> {  
} 

我還在我的項目中使用Spring Data Mongo和JPA。

當我運行該項目時,出現此錯誤。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lastDateController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.ReadingService com.innolabmm.software.mongotest.springrest.LastDateController.readingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.AccessReportRepository com.innolabmm.software.mongotest.springrest.ReadingService.reportRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accessReportRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property flush found for type void 

有沒有人有一個想法是怎麼回事?如果這有助於解決問題,我準備提供更多信息。提前致謝。

+0

我認爲你想要初始化的bean有一些依賴,這些依賴用注入初始化,你需要在願望bean中禁用DI,或者檢查依賴關係,也許它們指向一個不存在的資源 – 2013-07-01 09:11:48

回答

2

您是否使用Spring Boot?

我在Spring Boot應用程序中嘗試使用JPA和Mongo時引發了相同的異常。我發現JPA和Mongo都會解釋存儲庫,導致問題,因爲我的存儲庫特別擴展了JpaRepository

我只想要生成JPA存儲庫,所以將以下內容添加到應用程序入口點。

@EnableAutoConfiguration(exclude={MongoRepositoriesAutoConfiguration.class}) 
0

如果您使用的是帶有JPA的MongoDB(我JPA DB是MySQL的)這樣做:

  • 創建separete包域對象和存儲庫(DAO的):即demo.mysql.domain, demo.mysql.dao,demo.mongo.domain,在你的配置類使用demo.mongo.dao
  • 這些註釋:
 
    @EntityScan(basePackages={"demo.mysql.domain"}) 
    @EnableJpaRepositories(basePackages={"demo.mysql.dao"}) 
    @EnableMongoRepositories(basePackages={"demo.mongo.dao"}) 
  • 爲@Entity註解你的JPA實體,並把他們都在demo.mysql.domain包
  • 與@Document註解你的MongoDB實體,並把他們都在demo.mongo.domain包
  • 保持所有的MongoDB reporitories的在demo.mongo.dao包
  • 保持demo.mysql.dao包中的所有JPA存儲庫
  • 所有的mongo存儲庫都應該擴展MongoRepository(即公共接口TransactionRepository擴展MongoRepository)
  • 所有JPA庫應該延伸JpaRepository(即公共接口CityRepository擴展JpaRepository)

這是目前最簡單,我已經找到了乾淨的方式(通過文檔挖後,順便說一句)讓MongoDB和Mysql在一個Spring Boot應用程序中工作。

這就是我已經嘗試過,什麼起作用。您可以使用MongoTemplate或擴展CrudRepository而不是JpaRepository,或者其他任何方法,嘗試它,它可能會工作。

相關問題