Sus*_*ath 28 mongodb spring-data spring-data-mongodb
我有一个存储库接口,如下所示:
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);
}
Run Code Online (Sandbox Code Playgroud)
而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]
Run Code Online (Sandbox Code Playgroud)
理想情况下,尝试ObjectId使用无效字符串初始化时,应抛出此异常.[ 代码 ].我想知道如何初始化类以便在创建bean时抛出异常.关于这个的任何提示?
我遇到了同样的问题,以下应该可行。
@Query("{ 'student' : { '$id': ?0 } }")
List<ExcursionAttendee> findByStudentId(String studentId);
Run Code Online (Sandbox Code Playgroud)
如果它是一个 DBRef ,它应该是这样的,(这是我在同一问题后使用的,我的例子中有一个 DBRef )
@Query("{ 'student': {'$ref': 'user', '$id': ?0} }")
List<ExcursionAttendee> findByStudentId(String studentId);
Run Code Online (Sandbox Code Playgroud)
对于 DBRef 这应该也有效,
List<ExcursionAttendee> findByStudentId(String studentId);
Run Code Online (Sandbox Code Playgroud)
我从这里找到了答案。
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |