欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Property or field ‘XXX‘ cannot be found on null

程序员文章站 2022-07-15 16:16:19
...
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "name" (template: "admin/types-input" - line 32, col 71)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "name" (template: "admin/types-input" - line 32, col 71)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1021E: A problem occurred whilst attempting to access the property 'name': 'Unable to access property 'name' through getter method'
org.hibernate.LazyInitializationException: could not initialize proxy [com.athome.blog.pojo.Type#15] - no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:170) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
	at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]

已有开发环境如下

Windows平台

jdk1.8、maven已配置

开发工具:Intellij IDEA

在使用springboot 1.XX时,JpaRepository支持findOne(ID)方法

public Type getType(Long id) {
        return typeRepository.findOne(id);
    }
    或者
    public Type getType(Long id) {
        return typeRepository.getOne(id);
    }

但是在springboot 2.XX时findOne()方法不能这样简单使用,于是使用
findById()

public Type getType(Long id) {
        Optional<Type> type = typeRepository.findById(id);
        return type.get();
    }

完美解决