【问题标题】:JPA Query error creating beanJPA查询错误创建bean
【发布时间】:2016-02-15 23:22:28
【问题描述】:

当我将此查询添加到我的房间存储库时,我在启动 Spring 应用程序时开始收到错误消息。如果我对查询进行注释,错误就会消失并且应用程序会正确启动。

public interface RoomRepository extends JpaRepository<Room, Long> {

    @Query("select Room r from Room join r.bookings b where not ((b.beginDate >= :initDate And b.beginDate <= :endtDate) or (b.beginDate >= :initDate And b.endDate <= :endDate) or (b.beginDate <= :initDate and b.endDate >= :endDate) and b.approved = true and r.roomType = :roomType)")
    List<Room> findWithDates(@Param("initDate") Date initDate, @Param("endDate") Date endDate, @Param("roomType") long roomType);
}

错误:

Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.web.HttpMessageConverters org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.messageConverters; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private final java.util.List org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.converters; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingJackson2HttpMessageConverter' defined in class path resource [org/springframework/boot/autoconfigure/web/JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.fasterxml.jackson.databind.ObjectMapper]: : Error creating bean with name 'objectMapper' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceMappings' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.mapping.ResourceMappings]: Factory method 'resourceMappings' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roomRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findWithDates found for type Room!; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapper' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceMappings' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.mapping.ResourceMappings]: Factory method 'resourceMappings' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roomRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findWithDates found for type Room!

房间等级:

@Entity
public class Room {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    @ManyToOne
    @JoinColumn( name="roomType_id" )
    private RoomType roomType;

    @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="room", orphanRemoval = true)
    private Collection<Booking> bookings = new ArrayList<Booking>();

}

【问题讨论】:

  • 您的 @Query 注释 - 是 org.springframework.data.jpa.repository.Query 吗?
  • 您使用的是什么版本的 Spring 和 Spring Data JPA?您的代码适用于 Spring v4.0.9 和 Spring Data JPA v1.8.1。你可以在Github找到我的示例代码。

标签: java spring spring-mvc jpa spring-data-jpa


【解决方案1】:

您的方法名称与 Spring 自动实现的方法冲突(在您的情况下,Spring 查找名为“initDateEndDateRoomType”的字段并失败。

Spring Data 存储库基础结构中内置的查询构建器机制对于在存储库的实体上构建约束查询很有用。该机制从方法中去除前缀​​ find...By、read...By、query...By、count...By 和 get...By 并开始解析其余部分。

但是,如果您还扩展 JpaRepository&lt;Room, Long&gt;,它应该可以工作,但我尚未验证。

【讨论】:

  • 请让你的接口扩展JpaRepository&lt;Room, Long&gt;,它应该可以工作(如果你不扩展JpaRepository,似乎注释被忽略了)。
【解决方案2】:
@Query(value = "select Room r from Room join r.bookings b where not ((b.beginDate >= :initDate And b.beginDate <= :endtDate) or (b.beginDate >= :initDate And b.endDate <= :endDate) or (b.beginDate <= :initDate and b.endDate >= :endDate) and b.approved = true and r.roomType = :roomType)", nativeQuery=true)

这行得通,查询注释也是错误的。

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 2013-04-19
    • 2017-11-25
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2020-03-15
    • 2019-06-29
    • 1970-01-01
    相关资源
    最近更新 更多