darrachequesne / spring-data-jpa-datatables

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

Guidelines for using in a pre-existing Spring Boot Data application #37

Closed porterwoodward closed 8 years ago

porterwoodward commented 8 years ago

I saw this issue:

https://github.com/darrachequesne/spring-data-jpa-datatables/issues/1

Unfortunately the instructions never made it to the ReadMe. I'm in the process of using Spring Boot (along with it's JPA Data, and REST modules). With all of the lifting that Spring Boot does in this case most repositories are little more than annotated interfaces without a concrete implementation.

Any tips, tricks, or help in terms of how to get the datatables module configured and working alongside existing code would be extremely helpful. Currently I've tried a few of the approaches mentioned.

  1. Adding the properties to the configuration class: EnableJpaRepositories annotation to enable the DataTables factory, and indicating a base package to scan.
  2. Altering the repository interface to extend DataTablesRepository.
  3. Exposing a few of the properties of the entity via the JsonView annotation.

The context of course fails to load with the findAll method error mentioned in the initial issue above.

I then attempted to create a separate package, modified the package scan in the annotation, and added qualifiers to the interfaces (since with Spring Data, you're not defining implementations of the repository interfaces), and to the Autowiring annotations. Still no love - error with findAll not being found persisted.

Is it expected that this be able to work alongside, or as an adjunct to the existing Spring Data Rest capabilities.

dawnofclarity commented 8 years ago

Hi porterwoodward,

As a happy spring-data-jpa-datatables user, I can say that I have a similar setup to yourself working very well using an ultra-light configuration using only annotations (no XML) with Spring-boot-1.4.0, Spring 4.3.2, & Hibernate 5.1.0. As you describe, the repositories have no need of an impl, as spring data does the grunt work here.

For config:

@Configuration
@EnableJpaRepositories(
        basePackages = "xx.xx.xx.repository",
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager",
        repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class
)
@EnableTransactionManagement

In my entityManagerFactory bean I set the packages to scan

emf.setPackagesToScan("xx.xx.xx.repository")

In each repository where I want to use a dataTables call I extend from DataTablesRepository

@Repository
public interface CourseListRepository extends DataTablesRepository<CourseList, Integer> {
}

For ordinary repositories I leave them as JpaRepositoryas there are some subtle differences, for example, DataTablesRepository.findAll() returns an Iterable<?> rather than List<?> which is a pain if you want to use .stream().

The controller is simple:

    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    public DataTablesOutput<CourseList> list(@Valid DataTablesInput input) {
        return courseListRepository.findAll(input);
    }

I don't bother with @JsonView as it doesn't play well with Java 8 dates; instead I use @JsonIgnore on anything I don't want exposed.

porterwoodward commented 8 years ago

Thanks for the feed back, I'll give it a go; it just seemed like I was missing some configuration pieces. I'm using Boot 1.4.1 (Spring 4.3.3 and Hibernate 5.0.11), so hopefully it didn't introduce anything that broke anything.

I hadn't bothered to define an EntityManagerFactory - as everything was working - thanks for the pointers!

darrachequesne commented 8 years ago

@porterwoodward I hope @dawnofclarity's great answer will help you solve your issue. Else, could you please provide a sample project reproducing the issue?

@dawnofclarity thanks! For Java 8 dates, did you consider using jackson-datatype-jsr310 ?

darrachequesne commented 8 years ago

Closing due to inactivity, please reopen if needed.

M4veR1K78 commented 6 years ago

Hi,

As for myself, I'm unable to make it work in an existing Spring Boot application.

I have one interface extending DataTablesRepository, and all the others are classic PagingAndSortingRepository.

I have @EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class) on my application class. I tried dawnofclarity tips by adding additional parameters in @EnableJpaRepositories annotation and let the datatable repository have its own package, but nothing works. The No property findAll found for type <type> always occurs.

I wish there would be a complete example of an application with co-existing DatatablesRepository and Spring Data repositories.