SpelEvaluationException:EL1008E:在 Thymeleaf 应用程序中

SpelEvaluationException: EL1008E: in Thymeleaf app

我有一个基本的 Spring 启动应用程序。使用 Spring 初始化程序,嵌入 Tomcat,Thymeleaf 模板引擎,并打包为可执行 JAR 文件。

我有这个class

@Entity
@Table(name="t_tdk_device")
@DiscriminatorValue("tdk")
public class TdkDevice extends Device {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    public TdkDevice() {
        super();
    }

    public TdkDevice(String deviceKey, String devicePAC) {
        super(deviceKey, devicePAC);
    }

    @OneToMany(mappedBy = "device", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<DeviceDriver> driverDevices = new HashSet<>();

    public Set<DeviceDriver> getDriverDevices() {
        return driverDevices;
    }

    public void setDriverDevices(Set<DeviceDriver> driverDevices) {
        this.driverDevices = driverDevices;
    }

}

这个其他class

@Entity
@Table(name = "t_device_driver")
public class DeviceDriver implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public DeviceDriver() {

    }

    public DeviceDriver (Device device, Driver driver) {
        this.device = device;
        this.driver = driver;
    }


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "device_id")
    private Device device;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "driver_id")
    private Driver driver;

    public Device getDevice() {
        return device;
    }

    public void setDevice(Device device) {
        this.device = device;
    }

    public Driver getDriver() {
        return driver;
    }

    public void setDriver(Driver driver) {
        this.driver = driver;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((device == null) ? 0 : device.hashCode());
        result = prime * result + ((driver == null) ? 0 : driver.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DeviceDriver other = (DeviceDriver) obj;
        if (device == null) {
            if (other.device != null)
                return false;
        } else if (!device.equals(other.device))
            return false;
        if (driver == null) {
            if (other.driver != null)
                return false;
        } else if (!driver.equals(other.driver))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "DeviceDriver [device=" + device + ", driver=" + driver + "]";
    }
}

和另一个

@Entity
@Table(name="t_driver")
@DiscriminatorValue("Driver")
public class Driver extends Guardian implements Serializable  {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Driver() {
        super();
    }

    public Driver (User user) {
        super(user);
    }

    public Driver (User user, String trailerCode) {
        super(user);
        this.trailerCode=trailerCode;
    }

    @Column(name = "trailer_code")
    private String trailerCode;

    @OneToMany(mappedBy = "driver", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<DeviceDriver> driverDevices = new HashSet<>();


    public Set<DeviceDriver> getDriverDevices() {
        return driverDevices;
    }

    public void setDriverDevices(Set<DeviceDriver> driverDevices) {
        this.driverDevices = driverDevices;
    }

    public String getTrailerCode() {
        return trailerCode;
    }

    public void setTrailerCode(String trailerCode) {
        this.trailerCode = trailerCode;
    }

}

然后我想从 driverDevices 中提取所有驱动程序 但是当我从 thymeleaf 模板执行此操作时:

<select id="selectCompanyElementId"  >
                                            <option value="0">select</option>
                                            <option th:each="driver : ${device.driverDevices.driver}" 
                                                    th:value="${driver.id}" 
                                                    th:text="${driver.firstName}">
                                             </option>
                                    </select>

我收到这个错误:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'driver' cannot be found on object of type 'org.hibernate.collection.internal.PersistentSet' - maybe not public?

如我所见,设备 class 中的 driver 字段是一个对象,而不是一个集合。根据您的情况,它应该是 Set<Driver>

您必须在集合上使用 each 循环。类似于:

<option th:each="dd : ${device.driverDevices}" 
        th:value="${dd.driver.id}" 
        th:text="${dd.driver.firstName}">
</option>