Mongo Db数据库中的Spring JPA自定义方法实现不起作用

时间:2017-05-03 02:21:50

标签: spring-boot spring-data spring-data-jpa

我是Spring-JPA的新手,并试图围绕其工作方式。 我需要实现Mongo DB datetime字段的Spring JPA实现。我要做的就是执行以下查询的JPA等效。如果通知时间小于当前时间,我必须更新记录以将其标记为发送通知并发送了通知。但我甚至无法让春天跑。

这在JPA中是否可行。我已经看到了很多自定义sql查询,但在JPA的上下文中没有遇到自定义方法的 nosql 实现。

db.scheduledNotifications.find({"notificationTime":{$lte:new ISODate(?(now()}})

我从一些互联网指南和一些教程开始。

public interface ScheduledNotificationsRepositoryCustom
 {
    List<ScheduledNotifications> getNotification(Instant instant);

}

@Repository
@Transactional(readOnly=true)
public class ScheduledNotificationsRepositoryCustomImpl implements ScheduledNotificationsRepositoryCustom 
{
    @PersistenceContext
    EntityManager entityManager;
    @Override
    public List<ScheduledNotifications> getNotification(Instant instant) {
        // TODO Auto-generated method stub
        Query query=entityManager.createNativeQuery("{'notificationTime' : { '$lte' : ?0 }}", ScheduledNotifications.class);
        List<ScheduledNotifications> notifications=query.getResultList();
        return notifications;
    }

}

@Repository
public interface ScheduledNotificationsRepository extends JpaRepository<ScheduledNotifications, String>,ScheduledNotificationsRepositoryCustom 
{

}

在此之后,当我试图保存项目时,Spring正在抱怨抛出异常。 启动ApplicationContext时出错。要显示自动配置报告,请使用&#39; debug&#39;重新运行您的应用程序。启用。

2017-05-03 07:27:08.643 ERROR 11104 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduledNotificationController' defined in file [E:\lending\target\classes\hello\ScheduledNotificationController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [hello.ScheduledNotificationController] for resource metadata: could not find class that it depends on
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:526) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at hello.Application.main(Application.java:12) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]
Caused by: java.lang.IllegalStateException: Failed to introspect bean class [hello.ScheduledNotificationController] for resource metadata: could not find class that it depends on
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:344) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:297) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:992) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/jpa/repository/JpaRepository
    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_60]
    at java.lang.ClassLoader.defineClass(Unknown Source) ~[na:1.8.0_60]
    at java.security.SecureClassLoader.defineClass(Unknown Source) ~[na:1.8.0_60]
    at java.net.URLClassLoader.defineClass(Unknown Source) ~[na:1.8.0_60]
    at java.net.URLClassLoader.access$100(Unknown Source) ~[na:1.8.0_60]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0_60]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.8.0_60]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_60]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.findClass(RestartClassLoader.java:165) ~[spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:148) ~[spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_60]
    at java.lang.Class.getDeclaredFields0(Native Method) ~[na:1.8.0_60]
    at java.lang.Class.privateGetDeclaredFields(Unknown Source) ~[na:1.8.0_60]
    at java.lang.Class.getDeclaredFields(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:715) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:656) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:361) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:340) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 23 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.JpaRepository
    at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_60]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_60]
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_60]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_60]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) ~[spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_60]

... 42 common frames omitted

上面有任何一个错误。

pom.xml

     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
<build>

<plugins>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <configuration>
    <delimiters>
     <delimiter>${*}</delimiter><!-- to keep the default behavior -->
     <delimiter>@</delimiter><!-- to add Ant-like tokens style -->
    </delimiters>
  </configuration>
</plugin>
</plugins>
</build>
  <groupId>smenotification</groupId>
  <artifactId>lending</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>lending</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

  <dependencies>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    </dependency>
 <!--  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    </dependency> -->
   <!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>1.2.0.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
      <groupId>org.springframework.kafka</groupId>
      <artifactId>spring-kafka-test</artifactId>
     <scope>test</scope>
    </dependency>

    <!-- <dependency>
      <groupId>org.springframework.kafka</groupId>
      <artifactId>spring-kafka-test</artifactId>
      <scope>test</scope>
    </dependency> -->
  <dependency>

    <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
           </dependency>

    <!-- <dependency><groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
    <version>4.2.0.RELEASE</version>
    </dependency>  --> 
        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>

</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    </dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
                    </dependency>
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<!--   <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.9.0.1</version>
            <scope>provided</scope>
        </dependency> -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
  <groupId>org.influxdb</groupId>
  <artifactId>influxdb-java</artifactId>
  <version>2.5</version>
</dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cloud-connectors</artifactId>
    </dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

清理maven缓存解决了classnotfound问题。