我有一个存储库界面,如下所示:
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
时会抛出此异常。 [code]。我想知道如何初始化类以便在创建bean时抛出异常。关于这个的任何提示?
答案 0 :(得分:0)
我遇到了同样的问题,以下情况应该有效。
@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);
我从here找到答案。
答案 1 :(得分:0)
@Query("{ 'student._id' : {'$oid' : ?0} , 'excursionEvent._id' : { '$oid' : ?1 } }")
ExcursionAttendee findByStudentIdAndEventId(String studentId, String excursionId);
如果您遵循spring-data method naming conventions,则无需指定@Query。
答案 2 :(得分:0)
查询应与
一起使用@Query("{ 'student._id' : ?0} , 'excursionEvent._id' : ?1 } }")
参考:https://jira.spring.io/si/jira.issueviews:issue-html/DATAMONGO-1070/DATAMONGO-1070.html