Load entities by user

How can I display only the entities from CompanyReport that belong to the logged-in user?
each user belong to a customer. Each Customer have many Reports.

This is my Solution but not work:

    @ViewComponent
    private CollectionLoader<CompanyReport> companyReportsDl;

    @Autowired
    private CurrentAuthentication currentAuthentication;

    @Subscribe
    public void onInit(final InitEvent event) {
        User user = (User) currentAuthentication.getUser();
        Customer customer = user.getCustomer();
        if (customer != null){
            companyReportsDl.setCondition(PropertyCondition.equal("cusId", customer.getCusID()));
        }else {
            companyReportsDl.setCondition(PropertyCondition.equal("cusId", ""));
        }
        companyReportsDl.load();
    }

Hello,

You need to specify the name of your field in the PropertyCondition and pass an instance of the Customer class.

Controller:

    @Subscribe
    public void onInit(final InitEvent event) {
        User user = (User) currentAuthentication.getUser();
        Customer customer = user.getCustomer();
        if (customer != null) {
            reportsDl.setCondition(PropertyCondition.equal("customer", customer));
        }
        reportsDl.load();
    }

User entity:

    @JoinColumn(name = "CUSTOMER_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Customer customer;

Regards,
Nikita

1 Like