0

我有一个Market实体

@Entity
public class Market extends MutableEntity {

    @Column(nullable = false)
    private String name;
    @Column
    private String description;
    @Embedded
    private Version marketVersion; // snipped

Version实体为

@Embeddable
public class Version {
    private double versionNumber;
    private VersionType versionType;
    private DateTime publishedOn;
    private DateTime retiredOn;
    private double parentVersionNumber; //snipped

当我尝试测试以下内容时,它失败了

   @Test
    public void testCloneMarket() {
        final String name = "testCloneMarket";
        final String description = "testCloneMarket";
        final Market existingMarket = new MarketManager(crudService).addMarket(name, description);

        // publish market
        existingMarket.getMarketVersion().setPublishedOn(new DateTime()); // fails here


        assertNotNull(existingMarket);
    }

我看到错误为

testCloneMarket(com.myorg.project.versioning.business.MarketManagerTest)  Time elapsed: 4.11 sec  <<< ERROR!
javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90)
    at com.myorg.toolkit.commons.persistence_testing.rules.JpaRule.commitOrRollbackTransaction(JpaRule.java:220)
    at com.myorg.toolkit.commons.persistence_testing.rules.JpaRule.changeTransaction(JpaRule.java:155)
    at com.myorg.project.versioning.business.MarketManagerTest.testCloneMarket(MarketManagerTest.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    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 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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    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: javax.persistence.PersistenceException: org.hibernate.exception.DataException: A truncation error was encountered trying to shrink VARCHAR () FOR BIT DATA '(Binary data value not displayed)' to length 255.

我正在使用derby内存测试。为什么会发生这种情况,解决方法是什么?

谢谢

4

2 回答 2

0

我找不到setPublishedOn.

只有一个尝试!

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
existingMarket.getMarketVersion().setPublishedOn(dateFormat.format(date));
于 2013-02-11T20:13:59.060 回答
0

目前尚不清楚哪一列出现截断错误。

您可以尝试通过完全展开异常链从异常链中获取更多信息:请参阅此建议

或者,如果您可以找到您的 derby.log 文件,并且可以使用 -Dderby.language.logStatementText=true 运行您的应用程序,您应该能够看到更多详细信息。

Derby 数据类型VARCHAR FOR BIT DATA支持高达 32,672 字节的指定长度。没有默认长度,因此 256 字节长度必须是您在应用程序或用于实现持久层的框架之一中设置的值。

如果您可以确定持久层在哪里生成表模式,您应该能够覆盖它并为 VARCHAR FOR BIT DATA 列选择不同的长度。

于 2013-02-12T14:58:25.920 回答