Spring Jpa选择性更新

时间:2018-11-20 13:57:35

标签: spring hibernate spring-data-jpa

我想使用spring jpa有选择地更新表中的字段。根据{{​​3}},这应该可以通过简单的函数来实现,例如:

 public TestEntity update(UUID uuid, String string2new) {
    TestEntity testEntity = testEntityRepository.getOne(uuid);
    testEntity.setString2(string2new);
    return testEntityRepository.save(testEntity);
}

其中TestEntity是(省略了getter和setter):

@Entity
public class TestEntity {

    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Id
    @Column(columnDefinition = "BINARY(16)")
    private UUID id;

    private String string;
    private String string2;
}

但是,我的测试代码:

@Test
public void selectiveUpdate2() {
    TestEntity testEntity = testEntityRepository.save(TestEntity.builder().string("lulu").string2("lala").build());
    System.out.println(testEntityRepository.findAll());
    testEntityService.updateString2(testEntity.getId(), "lele");
    System.out.println(testEntityRepository.findAll());
}

但是我遇到如下异常:

  

[TestEntity(id = 61a24efc-877f-4a89-9e75-a9933fe7c04c,string = lulu,string2 = lala)]   org.hibernate.LazyInitializationException:无法初始化代理[com.example.test_db_zeug.TestEntity#61a24efc-877f-4a89-9e75-a9933fe7c04c]-无会话       在org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)       在org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)       在org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)       在org.hibernate.proxy.ProxyConfiguration $ InterceptorDispatcher.intercept(ProxyConfiguration.java:95)       com.example.test_db_zeug.TestEntity $ HibernateProxy $ GilcmZfo.setString2(未知来源)       在com.example.test_db_zeug.TestEntityService.update(TestEntityService.java:39)       在com.example.test_db_zeug.TestDbZeugApplicationTests.selectiveUpdate(TestDbZeugApplicationTests.java:27)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)       在org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)       在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)       在org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)       在org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)       在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)       在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)       在org.junit.runner.JUnitCore.run(JUnitCore.java:137)       在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)       在com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)       在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

0 个答案:

没有答案