I could not figure out how to refresh the ListDataProvider from the backend when the underlying data in the database is changed (i.e. after deleting a row with repository.delete() the entry is deleted from the mysql db, or changing a row manually through mysql console) dataprovider.refreshAll() method does not update the grid.
What I am missing?
If I set the grid dataprovider to lazy load from the callbacks, there is no refresh problem, however the grid does not seem to correctly list all entries this time
UI;
....
ListDataProvider<Account> ldp = DataProvider.ofCollection(service.findAll());
grid.setDataProvider(ldp);
...
// editing an entry works fine
grid.getEditor().setEnabled(true).addSaveListener(e -> {
Account bean = e.getBean();
service.save(bean);
});
grid.getEditor().setBuffered(true);
...
// refresh after deleting, or changing the entry in the DB directly does not update the grid
getBtn_refresh().addClickListener( event -> ldp.refreshAll() );
Service;
@Stateless
// The service needs to be marked as LocalBean if it implements an interface otherwise we get the error
// WELD-001408: Unsatisfied dependencies for type with qualifiers @Default when implementing interface
// when the application is deployed
@LocalBean
public class AccountService implements AbstractService<Account> {
@Inject AccountRepository repo;
public AccountService() {};
public Account save(Account entity) {
return repo.save(entity);
}
public void delete(Account entity) {
repo.delete(entity);
}
public int count() {
return Math.toIntExact(repo.count());
}
public List<Account> findAll() {
return repo.findAll();
}
public List<Account> findAllPaged(int offset, int limit) {
int page = offset / limit;
PageRequest pageRequest = new PageRequest(page, limit);
return repo.findAll(pageRequest).getContent();
}
public List<Account> findAllPaged(int offset, int limit, Map<String, Boolean> sortOrders) {
int page = offset / limit;
List<Sort.Order> orders = sortOrders.entrySet().stream()
.map(e -> new Sort.Order(e.getValue() ? Sort.Direction.ASC : Sort.Direction.DESC, e.getKey()))
.collect(Collectors.toList());
PageRequest pageRequest = new PageRequest(page, limit, orders.isEmpty() ? null : new Sort(orders));
return repo.findAll(pageRequest).getContent();
}
public Account refreshEntry(Account entry) {
return repo.findOne(entry.getId());
}
}
Repository (Spring Data);
@Eager
public interface AccountRepository extends JpaRepository<Account, Long> {
}