Search code examples
javaspringjpagridvaadin

Vaadin Grid. Problem with setting column based on entity property


I'm using spring and MySQL as database to ORM. Im trying to display entity properties in grid in one of my Views. Item Id is passed by Url, and Items are set after constructor. In this scenario I'm trying to display audits that given enterprise had in the past. When navigating to given view, exception is beeing thrown:

There was an exception while trying to navigate to 'EnterpriseView/151' with the root cause 'java.lang.IllegalArgumentException: Multiple columns for the same property: auditId

What does it mean, as when I'm checking columns in database there in only one auditId in audit Table?

There are my classes:

import com.sun.istack.NotNull;
import javax.persistence.*;

@Entity
@Table
public class Audit {

    private int auditId;

    private Trip trip;

    private User user;

    private Enterprise enterprise;

    public Audit() {
    }

    public Audit(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    @Id
    @GeneratedValue
    @NotNull
    @Column(unique = true)
    public int getAuditId() {
        return auditId;
    }

    @ManyToOne
    @JoinColumn(name = "TRIPS_ID")
    public Trip getTrip() {
        return trip;
    }

    @ManyToOne
    @JoinColumn(name = "USER_ID")
    public User getUser() {
        return user;
    }

    @ManyToOne
    @JoinColumn(name = "ENTERPRISE_ID")
    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setAuditId(int auditId) {
        this.auditId = auditId;
    }

    public void setTrip(Trip trip) {
        this.trip = trip;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }
}
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.*;
import com.wtd.assistant.frontend.dao.AuditDao;
import com.wtd.assistant.frontend.dao.EnterpriseDao;
import com.wtd.assistant.frontend.domain.Audit;
import com.wtd.assistant.frontend.domain.Enterprise;

import java.util.List;
import java.util.Optional;

@Route("EnterpriseView")
public class EnterpriseView extends VerticalLayout implements HasUrlParameter<String>, AfterNavigationObserver{

    private EnterpriseDao enterpriseDao;
    private AuditDao auditDao;
    private Grid<Audit> grid;
    private List<Audit> auditsList;
    private Optional<Enterprise> enterprise;
    private String enterpriseId;

    public EnterpriseView(EnterpriseDao enterpriseDao, AuditDao auditDao) {
        this.enterpriseDao = enterpriseDao;
        this.auditDao = auditDao;
        this.grid = new Grid<>(Audit.class);

        VerticalLayout layout = new VerticalLayout();
        layout.add(grid);
        grid.addColumns( "auditId" );
    }

    @Override
    public void setParameter(BeforeEvent event, String parameter) {
        enterpriseId = parameter;
        System.out.println("setParameter(), enterpriseId: " + enterpriseId);
    }

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        enterprise = enterpriseDao.findById(Integer.valueOf(enterpriseId));
        System.out.println("EnterpriseId: " + enterprise.get().getEnterpriseId());
        auditsList = enterprise.get().getAudits();
        grid.setItems(auditsList);
    }
}

I tried renaming auditId property but obviously that didn't bring any result

Kind regards Kiemoon


Solution

  • In the constructor of the EnterpriseView you have this code:

    grid.addColumns( "auditId" );
    

    Thats where your duplicate is comming from