EL1021E:尝试访问该属性时发生问题

时间:2020-11-04 17:26:32

标签: java mysql spring-boot spring-data thymeleaf

我无法访问百里香中另一个对象(讲座)中嵌套对象(用户)的字段(用户名)。但是访问用户ID没问题。 有什么想法吗?

     @Entity
     public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        
        @NotBlank
        @Column(nullable = false, unique = true)
        private String username;
    
        public User() {}
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
}

Lecture.java

    @Entity
    public class Lecture {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @ManyToOne(fetch = FetchType.LAZY)
        private User speaker;
    
        private String title;
    
        public Lecture() {
        }
        
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
        
        public void setSpeaker(User speaker) {
            this.speaker = speaker;
        }
    
        public User getSpeaker() {
            return speaker;
        }
    }

index.html的一部分

<tbody>
     <tr th:each="lecture : ${lecturesPage}">
         <td><a th:href="@{'/lecture/' + ${lecture.id}}" th:text="${lecture.id}"></a></td>
         <td th:text="${lecture.speaker.username}" /> <-- not working -->
         <td th:text="${lecture.speaker.id}" /> <-- working -->
         <td th:text="${lecture.title}" />
     </tr>
</tbody>

控制台:

org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:“ lecture.speaker.username”

由以下原因引起:org.springframework.expression.spel.SpelEvaluationException:EL1021E:尝试访问属性“用户名”时出现问题:“无法通过getter方法访问属性“用户名””

起因:org.springframework.expression.AccessException:无法通过getter方法访问属性“用户名”

原因:org.hibernate.LazyInitializationException:无法初始化代理[com.springbootlecturewebapp.springbootlecturewebapp.model.dao.User#1]-没有会话

1 个答案:

答案 0 :(得分:0)

已删除(提取= FetchType.LAZY)正在工作

@Entity
    public class Lecture {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @ManyToOne // Removed (fetch = FetchType.LAZY)
        private User speaker;
}