darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
447 stars 173 forks source link

wiring into existing spring-boot app fails: "No property findAll found for type..." #1

Closed tomcgn closed 8 years ago

tomcgn commented 8 years ago

Hello Damien, spring-data-jpa-datatables is just what I was looking for. I was about to implement a direct connector between datatables and a spring rest repository, when I found it. I could run the example project and it works just fine. I'll post an enhancement for easier testing (CORS Headers) later on. I'd like to integrate your approach into an existing app. I added the dependency and the annotation

@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class) to the Application class.

When I start the application, an exception is thrown: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Meeting...

In the Interface @Repository public interface MeetingRepository extends PagingAndSortingRepository<Meeting, Long> several method signatures exist, and I added a DataTablesOutput<Meeting> findAll(DataTablesInput input);

The app uses Spring Boot Starter 1.2.4 . Any ideas why the discovery of the repository methods fails?

darrachequesne commented 8 years ago

Hi! Glad you find it useful! Don't hesitate if you find something is missing.

My first guess is that your MeetingRepository should extend DataTablesRepository (which itself extends PagingAndSortingRepository). The findAll method is defined there https://github.com/darrachequesne/spring-data-jpa-datatables/blob/master/src/main/java/org/springframework/data/jpa/datatables/repository/DataTablesRepository.java#29

Cheers

tomcgn commented 8 years ago

Hi Damien, Thanks for the quick response. And for the very good work!

Sure, I didn't miss the inheritance so the reason was something else.

The autowiring of the existing repositories failed, if the application is configured to use @EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class) for all repos. The solution is to create new repos that extend the exisiting ones, in a distinct package - anyways a good idea to keep existing api stable, and have these repositories interfaces extend the DataTablesRespository, as you suggest.

Then, you can specify that package in Application.java @EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class, basePackages = "org.whatever.repository.datatables") Then the differing factory is only applied to the repositories responsible for datatables.

Sorting and paging works fine now. I will continue tomorrow with the search, which does not yet work (it's supported, right?)

Thanks again for sharing this, it was definitely missing and is worth a mention on datatables.net

darrachequesne commented 8 years ago

Well, it seems my answer was, if quick, not really useful :smile:

Your solution is great, I think it's worth a mention in the readme! You could also create your own RepositoryFactory (disclaimer: your solution is cleaner):

private static class YourCustomRepositoryFactory<T, ID extends Serializable>
            extends JpaRepositoryFactory {
        [...]

        @SuppressWarnings({ "unchecked" })
        protected Object getTargetRepository(RepositoryMetadata metadata) {
            JpaEntityInformation<T, Serializable> entityInformation = (JpaEntityInformation<T, Serializable>) getEntityInformation(metadata
                    .getDomainType());
            Class<?> repositoryInterface = metadata.getRepositoryInterface();

            return DataTablesRepository.class.isAssignableFrom(repositoryInterface)
                    ? new DataTablesRepositoryImpl<T, ID>(entityInformation, entityManager)
                    : <your other implementation here>;
        }
    }

PS: search should be working, maybe some column must be set with searchable: false on the client-side (yet another clueless guess!).

Cheers

tomcgn commented 8 years ago

Well, your last one was, if only I followed your advice: Live seach works as intended if disable searching on all except one coiumn! That will do for the moment, thanks again :) I will leave the other issue open, as it adresses a slightly different topic (search on all filed of an entitiy rather than on one column/member of the model class. Cheers

shubham-aggarwal commented 7 years ago

Just specifying repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class in my Application class did the trick.