有 Java 编程相关的问题?

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

java EL1021E:尝试访问属性时出现问题

我在访问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;
        }
}

演讲。爪哇

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

索引的一部分。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>

控制台:

组织。百里香。例外情况。TemplateProcessingException:计算SpringEL表达式的异常:“讲座.演讲者.用户名”

原因:org。springframework。表示斯佩尔。SpelEvaluationException:EL1021E:尝试访问属性“username”时出现问题:“无法通过getter方法访问属性“username”

原因:org。springframework。表示AccessException:无法通过getter方法访问属性“username”

原因:org。冬眠LazyInitializationException:无法初始化代理[com.springbootExceptionWebApp.springbootExceptionWebApp.model.dao.User#1]-无会话


共 (1) 个答案

  1. # 1 楼答案

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

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