0

I know that such questions were asked (Did not find handler method ), but after trying multiple solutions I'm still stucked. So my problem is: I can't use both REST and JPA in my project. com.db.ruf: WRepository.class:

@NoRepositoryBean
@Component
public interface WRepository <T, ID extends Serializable>
        extends JpaRepository<T, ID> {
}

com.db.ruf: WRepositoryImpl.class:

public class WRepositoryImpl<T, ID extends Serializable>
        extends SimpleJpaRepository<T, ID> implements WRepository<T, ID> {

    private EntityManager entityManager;

    // There are two constructors to choose from, either can be used.
    public WRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);

        // This is the recommended method for accessing inherited class dependencies.
        this.entityManager = entityManager;
    }
}

com.db: MyRepositoryFactoryBean.class

public class MyRepositoryFactoryBean <R extends JpaRepository<T, I>, T, I extends Serializable>
        extends JpaRepositoryFactoryBean<R, T, I> {
    /**
     * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public MyRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

        return new MyRepositoryFactory(entityManager);
    }

    private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

        private EntityManager entityManager;

        public MyRepositoryFactory(EntityManager entityManager) {
            super(entityManager);

            this.entityManager = entityManager;
        }

        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return new WRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
        }

        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

            // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
            //to check for QueryDslJpaRepository's which is out of scope.
            return WRepository.class;
        }
    }
}

com.rest.: WebadminRESTController.class

@RestController
@Component

public class WebadminRESTController {

    @Autowired
   WRepository<ExternalLink, Long> wRepositoryImpl;

    @RequestMapping(value = "/allExternalLinks", method = RequestMethod.GET)
    public ResponseEntity<?> allExternalLinks() {
...
     }
}

com:

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.db.ruf", repositoryFactoryBeanClass = MyRepositoryFactoryBean.class)
@ComponentScan(basePackages = "com", resourcePattern = "com.*")

@EntityScan({"com.db"})


public class WebadminApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebadminApplication.class, args);
    }
}

In this case I get:

Did not find handler method for [/allExternalLinks]

If I change @ComponentScan(basePackages = "com", resourcePattern = "com.*") to @ComponentScan(basePackages = "com") or @ComponentScan I get:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.db.ruf.WRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I really don't know what's wrong. Could anyone be so kind to explain it? Thank you in advance

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • Vera, please explain, what exactly do you want to do? It is enough only 3 classes to work with data-jpa and data-jpa-rest... Did you read this [manual](https://spring.io/guides/gs/accessing-data-rest/)? – Cepr0 Jul 11 '17 at 17:18
  • The deal is that I have problem with a cache of entityManager. Whan I called "save" using simple "interface RepoName extends CrudRepository", I got incorrect data because the saving entity is additionally changed by db trigger. I need to call refresh or something like that.So according https://docs.spring.io/spring-data/jpa/docs/1.4.0.M1/reference/html/repositories.html I tried to implement methods above. – Vera Geraskina Jul 12 '17 at 06:27
  • Я создал [чат](https://chat.stackoverflow.com/rooms/148969), можем пообщаться там... – Cepr0 Jul 12 '17 at 07:42
  • Мне очень жаль, но не выйдет: You must have 20 reputation on Stack Overflow to talk here. – Vera Geraskina Jul 12 '17 at 09:36
  • How about here: https://gitter.im/stack-over-flow/SpringDataJPA-REST ? – Cepr0 Jul 12 '17 at 09:54

2 Answers2

0

Use @Repository on WRepositoryImpl.java. And edit WebadminRESTController.java:

@Autowired WRepositoryImpl<ExternalLink, Long> wRepositoryImpl;

  • When I do that I get: > org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} – Vera Geraskina Jul 12 '17 at 06:17
  • Try `@Service` on your WRepositoryImpl class, and delete `@Component` from your interface. In your WebadminRESTController class, edit the specified line to `@Autowired WRepositoryImpl wRepositoryImpl;` – cuongnguyen Jul 12 '17 at 08:55
  • Sorry, try `@Repository` – cuongnguyen Jul 12 '17 at 09:02
  • > org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​ion: No qualifying bean of type 'java.lang.Class>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} .....( – Vera Geraskina Jul 12 '17 at 09:33
0

Accidentally I found solution (don't think that it is the best one, but at least it works):

public interface  ExternalLinksRepo extends  CrudRepository<ExternalLink, Long> {
...
}

and:

public class WebadminRESTController {

   @Autowired
   ExternalLinksRepo wRepositoryImpl;
...}

Then wRepositoryImpl is automatically created as instance of WRepositoryImpl

Thanks to all, especially to Cepr0