Spring Data Jpa 使用 getOne(String id) 方法 报 no session 的错误

Spring Data Jpa 使用 getOne(String id) 方法 报 no session 的错误

今天使用 Spring Data JPA 写了个demo 遇到一个问题: 是关于 getOne(String id) 和findOne(String id) 的.

问题如下:

ShopConfigRepository 如下:

public interface ShopConfigRepository extends JpaRepository<ShopConfigEntity,String> {


}

接下来使用

   @Autowired
    private ShopConfigRepository shopConfigRepository;

    @Test
    public void test(){

        ShopConfigEntity shopConfigEntity = shopConfigRepository.findOne("1");
        log.info("findOne = {}",shopConfigEntity.toString());

        ShopConfigEntity shopConfigEntity1 = shopConfigRepository.getOne("1");
        log.info("findOne = {}",shopConfigEntity1.toString());



    }

上面这两个方法, 分别调用了 shopConfigRepository.getOne("1"); 和 shopConfigRepository.getOne("1"); 方法.

但是问题来了:

当使用 shopConfigRepository.getOne("1"); 方法的时候报了下面的错误:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:148)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
    at com.yearcon.pointshop.moudles.user.entity.ShopConfigEntity_$$_jvst32d_0.toString(ShopConfigEntity_$$_jvst32d_0.java)
    at com.yearcon.pointshop.shopconfig.ShopConfigTest.test(ShopConfigTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

使用 shopConfigRepository.findOne("1"); 就不会报错.

不知道怎么回事? 望知情者解惑,万分感谢.

蛊毒传说
浏览 1820回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java