Hibernate在merge()操作上插入空实体

时间:2017-05-17 06:02:35

标签: java hibernate jpa

我在实体图中为root元素调用函数merge()。实体是级联的。在图中,有持久和瞬态元素。我插入了一个空的额外实体。实体:

java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
    at org.apache.http.impl.auth.BasicScheme.authenticate(BasicScheme.java:166)
    at org.apache.http.impl.auth.HttpAuthenticator.doAuth(HttpAuthenticator.java:239)
    at org.apache.http.impl.auth.HttpAuthenticator.generateAuthResponse(HttpAuthenticator.java:202)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:263)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:343)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:174)
    at step_definitions.STRAXDefinition.setup(STRAXDefinition.java:42)
    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 cucumber.runtime.Utils$1.call(Utils.java:37)
    at cucumber.runtime.Timeout.timeout(Timeout.java:13)
    at cucumber.runtime.Utils.invoke(Utils.java:31)
    at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:59)
    at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:222)
    at cucumber.runtime.Runtime.runHooks(Runtime.java:210)
    at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:200)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.runtime.junit.ExamplesRunner.run(ExamplesRunner.java:59)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.runtime.junit.ScenarioOutlineRunner.run(ScenarioOutlineRunner.java:53)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:93)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:37)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:98)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 83 more

控制器:

@Entity
public class CnTermsAmortization {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id;

    @ManyToOne
    private CnTermsCounted cnTermsCounted;

    @ManyToOne
    private CnTermsBudget cnTermsBudget;

    ...

}

@Entity
public class CnTermsBudget {

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @OneToMany(mappedBy = "cnTermsBudget")
    Set<CnTermsAmortization> amortizations = new HashSet<>();

    @ManyToOne
    private CnTermsGroup cnTermsGroup;  

    .....   

}

@Entity
public class CnTermsCounted {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id;

    @OneToMany(mappedBy = "cnTermsCounted", cascade=CascadeType.ALL, orphanRemoval=true)
    private Set<CnTermsAmortization> amortizations = new HashSet<>();

    @ManyToOne
    private CnTermsGroup cnTermsGroup;
    .....
}

@Entity
public class CnTermsGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id;        

    @OneToMany(mappedBy="cnTermsGroup", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<CnTermsCounted> cnTermsCounteds = new ArrayList<>();

    @OneToMany(mappedBy="cnTermsGroup", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<CnTermsBudget> cnTermsBudgets = new ArrayList<>();

    .....
}

在Hibernate日志中,我看到插入了两个实体:

Hibernate:插入CnTermsAmortization(cnTermsBudget_id,cnTermsCounted_id,id)值(?,?,?) Hibernate:插入CnTermsAmortization(cnTermsBudget_id,cnTermsCounted_id,id)值(?,?,?)

在控制器输出中,我看到其中一个在所有字段中都为空:

[CnTermsAmortization [id = 1,cnTermsCounted=entities.CnTermsCounted@47747fb9,cnTermsBudget = entity.CnTermsBudget @ 51684e4a],CnTermsAmortization [id = 2,cnTermsCounted = null,cnTermsBudget = null]] < / p>

当我在图的根上调用merge(),其中所有实体都是Transient时,所有实体都正常工作 - 在输出中我有一个具有填充值的实体。我使用Hibernate版本5.1.1,JPA接口。 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

这是错误。它已被修复。有关bugfix的信息可在此处获得:https://hibernate.atlassian.net/browse/HHH-9512