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

Is it possible to use specification for view? #147

Closed fooo19 closed 1 year ago

fooo19 commented 1 year ago

I am currently looking to display data in a DataTable from view by PostgeSQL. I have created an Entity class for the view and a Repository that extends DataTablesRepository, just like a normal table.

The standard findAll(dataTablesInput) retrieves the data successfully, but if I give it an additional specification, I cannot retrieve the data.

Entity

@Entity  
@Data  
@Immutable  
@Table(name="table_view")  
public class TableView {  

    @Id  
    @Column(name="id")  
        private int id;  

    @Column(name="event_time")  
    private Timestamp eventTime;  

        //other columns  
        …  
}

Repository

@Repository
public interface TableViewRepository extends DataTablesRepository<TableView, Integer>{

}

Service

DataTablesOutput<TableView> dataList = new DataTablesOutput<TableView>();
dataList = tableViewRepository.findAll(input, specification);`

This is the method that generates the specification which retrieves data within a specified time period.

public Specification<TableView> additionalSpecification(Timestamp dateFrom, Timestamp dateTo) throws Exception{
    return new Specification<TableView>() {
        @Override
        public Predicate toPredicate(Root<TableView> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {

            return criteriaBuilder.between(root.get("eventTime"), dateFrom, dateTo);
        }
    };
}

DataTabelsOutput after execution is as follows DataTablesOutput(draw=2, recordsTotal=11, recordsFiltered=0, data=[], searchPanes=null, error=null)

When the same code is executed for a regular table, the data is successfully retrieved. Do I need to do anything special to use specification for view?

fooo19 commented 1 year ago

Sorry, the problem was on the front side. The data was incorrectly set up and we were not getting new data. I close this ticket.

darrachequesne commented 1 year ago

Hi! Thanks for the update, views should indeed work properly.