有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SpelEvaluationException:EL1008E:在Thymeleaf应用程序中

我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包

我有这门课

@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;
    }

}

另一类

@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?

共 (2) 个答案

  1. # 1 楼答案

    必须在集合上使用each循环。比如:

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

    正如我所看到的,设备类中的driver字段是一个对象,而不是一个集合。这对你的处境应该是一个Set<Driver>