Можно ли использовать метод findbyId в ingiteRepository? - PullRequest
0 голосов
/ 13 марта 2020

Я хочу использовать IgniteRepository и метод запроса. Метод «save» и метод «findAll» - это нормально, но когда я пытаюсь найти данные по id, у меня возникают проблемы с использованием метода «findById». Можно ли использовать метод запроса путем зажигания хранилища? (В кэше Ignite используется формат Key-Value. Мне также интересно узнать идентификатор в «findById» в IgniteRepository, означающий значение ключа или @id.)

  1. Ниже приводится сообщение об ошибке.

    java.lang.IllegalArgumentException: No fragment found for method public abstract java.util.Optional org.springframework.data.repository.CrudRepository.findById(java.lang.Object)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.lambda$null$2(RepositoryComposition.java:353) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_232]
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.lambda$invoke$3(RepositoryComposition.java:353) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324) ~[na:1.8.0_232]
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:348) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at com.sun.proxy.$Proxy145.findById(Unknown Source) ~[na:na]
    at kr.co.ignite.api.MyRestController.getDog(MyRestController.java:68) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_232]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_232]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_232]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_232]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_232]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_232]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.21.jar:9.0.21]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_232]
    
  2. Ниже следует сущность.

    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    import org.apache.ignite.cache.query.annotations.QuerySqlField;
    
    import lombok.AccessLevel;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @NoArgsConstructor(access = AccessLevel.PUBLIC)
    @ToString
    @Data
    @Entity
    public class Dog implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @QuerySqlField(index = true)
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @QuerySqlField(index = true)
        private String name;
    
        @QuerySqlField(index = true)
        private Date birthday;
    }
    
  3. Ниже следует хранилище, расширяющее IgniteRepository.

    import org.apache.ignite.springdata.repository.IgniteRepository;
    import org.apache.ignite.springdata.repository.config.RepositoryConfig;
    
    import kr.co.ignite.model.Dog;
    
    @RepositoryConfig(cacheName = "DogCache")
    public interface DogRepository extends IgniteRepository < Dog, Long > {
    }
    
  4. Ниже приводится конфигурация зажигания.

    import org.apache.ignite.Ignite;
    import org.apache.ignite.Ignition;
    import org.apache.ignite.configuration.CacheConfiguration;
    import org.apache.ignite.configuration.IgniteConfiguration;
    import org.apache.ignite.springdata.repository.config.EnableIgniteRepositories;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.repository.query.QueryLookupStrategy;
    
    import kr.co.ignite.model.Breed;
    import kr.co.ignite.model.Dog;
    import kr.co.ignite.repository.BreedRepository;
    import kr.co.ignite.repository.DogRepository;
    
    @Configuration
    @EnableIgniteRepositories(basePackageClasses = {BreedRepository.class, DogRepository.class},
    queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
    public class SpringAppConfig {
    
        @Bean
        public Ignite igniteInstance() {
            IgniteConfiguration cfg = new IgniteConfiguration();
    
            // Setting some custom name for the node.
            cfg.setIgniteInstanceName("springDataNode");
            // Enabling peer-class loading feature.
            cfg.setPeerClassLoadingEnabled(true);
            cfg.setLocalHost("127.0.0.1");
    
    
            // Defining and creating a new cache to be used by Ignite Spring Data
            // repository.
            CacheConfiguration<Long, Dog> ccfgDog = new CacheConfiguration<Long, Dog>("DogCache");
            CacheConfiguration<Long, Breed> ccfgBreed = new CacheConfiguration<Long, Breed>("BreedCache");
            // Setting SQL schema for the cache.
            ccfgBreed.setIndexedTypes(Long.class, Breed.class);
            ccfgDog.setIndexedTypes(Long.class, Dog.class);
            cfg.setCacheConfiguration(new CacheConfiguration[] { ccfgDog, ccfgBreed });
    
            return Ignition.start(cfg);
        }
    }
    
  5. Ниже приводится контроллер.

    import java.util.Date;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.PutMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import kr.co.ignite.model.BeanUtil;
    import kr.co.ignite.model.Dog;
    import kr.co.ignite.model.UETenancy;
    import kr.co.ignite.repository.DogRepository;
    import kr.co.ignite.repository.TenancyRepository;
    
    
    @RestController
    @RequestMapping(value = "/api")
    public class MyRestController {
    
        private long increment = 0L;
    
        /* */
        @PostMapping(value = "/ignite")
        public ResponseEntity<String> addDog(@RequestBody Dog dog) throws Exception {
    
            DogRepository dogRepository = BeanUtil.getBean(DogRepository.class);
    
            dog.setId(increment);
            dog.setBirthday(new Date());
    
            dogRepository.save(increment++, dog);
    
            return ResponseEntity.ok().body("ok");
        }
    
        /* */
        @GetMapping(value = "/ignite")
        public ResponseEntity<Iterable<Dog>> getDogs() throws Exception {
    
            DogRepository dogRepository = BeanUtil.getBean(DogRepository.class);
    
            return ResponseEntity.ok().body(dogRepository.findAll());
        }
    
        /* */
        @GetMapping(value = "ignite/{id}")
        public ResponseEntity<Dog> getDog(@PathVariable long id) throws Exception {
    
            DogRepository dogRepository = BeanUtil.getBean(DogRepository.class);
    
            return ResponseEntity.ok().body(dogRepository.findById(id).get());
        }
    
        /* */
        @PutMapping(value = "/ignite/{id}")
       public ResponseEntity<Dog> editDog(@RequestBody Dog dog, @PathVariable long id) throws Exception {
    
            DogRepository dogRepository = BeanUtil.getBean(DogRepository.class);
    
            Dog dogToEdit = dogRepository.findById(id).get();
            dogToEdit.setName(dog.getName());
            dogToEdit.setBirthday(dog.getBirthday());
            return ResponseEntity.ok().body(dogRepository.findById(dogToEdit.getId()).get());
        }
    
        @DeleteMapping(value = "/ignite/{id}")
        public ResponseEntity<String> removeDog(@PathVariable long id) throws Exception {
    
            DogRepository dogRepository = BeanUtil.getBean(DogRepository.class);
    
            dogRepository.deleteById(id);
    
            return ResponseEntity.ok().body("ok");
        }
    }
    
...