Spring Mongo数据仓库接口出现“无效的十六进制表示”。

29

我有一个如下的存储库接口:

public interface ExcursionAttendeeRepository extends MongoRepository<ExcursionAttendee, String> {

    ExcursionAttendee findByWorkflowItemId(String workflowItemId);

    @Query("{ 'excursionEvent._id' : { '$oid' : ?0 } }")
    List<ExcursionAttendee> findByExcursionId(String excursionId);

    @Query("{ 'student._id' : {'$oid' : ?0} , 'excursionEvent._id' : { '$oid' : ?1 } }")
    ExcursionAttendee findByStudentIdAndEventId(String studentId, String excursionId);

    @Query("{ 'student._id' : { '$oid' : ?0 } }")
    List<ExcursionAttendee> findByStudentId(String studentId);
}

在创建Bean时,Spring会抛出以下异常。

Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer#0.1':
  Cannot create inner bean '(inner bean)#5172829b' of type [org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter] while setting bean property 'messageListener';
  nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5172829b':
    Cannot resolve reference to bean 'productDetailsEventConsumer' while setting bean property 'delegate';
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productDetailsEventConsumer': Injection of autowired dependencies failed;
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cinglevue.veip.service.excursion.ExcursionAttendeeService com.cinglevue.veip.service.erp.impl.ProductDetailsEventConsumer.excursionAttendeeService;
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'excursionAttendeeServiceImpl': Injection of autowired dependencies failed;
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cinglevue.veip.repository.excursion.ExcursionAttendeeRepository com.cinglevue.veip.service.excursion.impl.ExcursionAttendeeServiceImpl.excursionAttendeeRepository;
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'excursionAttendeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [_param_0]
理想情况下,当试图使用无效字符串初始化ObjectId时,应该抛出这些异常。[代码]。我想知道在bean创建时如何初始化类以便抛出异常。有什么提示吗?
3个回答

1
我遇到了同样的问题,下面的方法应该可以解决。
@Query("{ 'student' : { '$id': ?0 } }")
List<ExcursionAttendee> findByStudentId(String studentId);

如果是一个DBRef,应该像这样(这是我在遇到相同问题后使用的方法,我的情况中有一个DBRef)
@Query("{ 'student': {'$ref': 'user', '$id': ?0} }")
List<ExcursionAttendee> findByStudentId(String studentId);

对于DBRef,这也应该有效。
List<ExcursionAttendee> findByStudentId(String studentId);

我从这里找到了答案。

0
 @Query("{ 'student._id' : {'$oid' : ?0} , 'excursionEvent._id' : { '$oid' : ?1 } }")
ExcursionAttendee findByStudentIdAndEventId(String studentId, String excursionId);

如果您遵循Spring Data方法命名约定,则无需指定@Query。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接