相关疑难解决方法(0)

LazyInitializationException:无法初始化代理 - 没有会话

我用spring-data-jpaspring-boot(v2.0.0.RELEASE),只是写在MySQL凝乳演示,但它如下发生运行时异常,源代码如下:

源代码

User.java

@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;
    private String username;
    private String password;

    ...getter&setter
} 
Run Code Online (Sandbox Code Playgroud)

UserRepository.java

public interface UserRepository extends JpaRepository<User, Integer> {

}
Run Code Online (Sandbox Code Playgroud)

UserServiceTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void getUserById() throws Exception{
        userRepository.getOne(1);
    }

}
Run Code Online (Sandbox Code Playgroud)

application.yml

spring:
  datasource:
    username: ***
    password: ***
    driver-class-name: com.mysql.jdbc.Driver
    url: ********
  thymeleaf:
    cache: false
  jpa:
    show-sql: true
    hibernate: …
Run Code Online (Sandbox Code Playgroud)

java spring-data spring-data-jpa spring-boot

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

spring-boot ×1

spring-data ×1

spring-data-jpa ×1