2013-03-28 119 views
1

我遇到了涉及postgres jdbc驅動程序版本9.1-901.jdbc3的奇怪問題。 這裏是吾道Hibernate 4和Postgres 9:Criteria.list()拋出異常

@Override 
    public List<Products> getProductsListByIds(List<String> ids, Integer storeId) { 
    Criteria criteria = getSession().createCriteria(Products.class) 
    .add(Restrictions.eq("storeId", storeId)) 
    .add(Restrictions.in("productId", ids)) 
    .add(Restrictions.isNotEmpty("createdAt")) 
    .add(Restrictions.isNotEmpty("updatedAt")) 
    .add(Restrictions.isNotNull("id")) 
    return criteria.list(); 
    } 

當我運行SQL語句返回96分的結果和我猜的100 Hibernate有固定列表大小(即我多少IDS通過限制),並返回在4個空對象名單。 作爲結果的Postgres JDBC驅動程序時,它試圖映射空到時間戳與下面的異常失敗:

org.hibernate.exception.DataException: Bad value for type timestamp : 
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:134) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:108) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
    at com.sun.proxy.$Proxy44.getTimestamp(Unknown Source) 
    at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$2.doExtract(TimestampTypeDescriptor.java:67) 
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65) 
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261) 
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) 
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247) 
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332) 
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2873) 
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1668) 
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1600) 
    at org.hibernate.loader.Loader.getRow(Loader.java:1500) 
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:712) 
    at org.hibernate.loader.Loader.processResultSet(Loader.java:940) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:910) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) 
    at org.hibernate.loader.Loader.doList(Loader.java:2516) 
    at org.hibernate.loader.Loader.doList(Loader.java:2502) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332) 
    at org.hibernate.loader.Loader.list(Loader.java:2327) 
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1621) 
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 
    at com.insparq.insparqsvc.dao.impl.StoresDAOImpl.getProductsListByIds(StoresDAOImpl.java:115) 
    at com.insparq.insparqsvc.service.impl.StoreServiceImpl.getProductsListByIds(StoreServiceImpl.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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at com.sun.proxy.$Proxy38.getProductsListByIds(Unknown Source) 
    at com.insparq.jobserver.service.impl.ScheduledJobServiceImpl.normalizeProducts(ScheduledJobServiceImpl.java:446) 
    at com.insparq.jobserver.service.impl.ScheduledJobServiceImpl.parseCSV(ScheduledJobServiceImpl.java:415) 
    at com.insparq.jobserver.service.impl.ScheduledJobServiceImpl.generateProductsCSV(ScheduledJobServiceImpl.java:214) 
    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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198) 
    at com.sun.proxy.$Proxy39.generateProductsCSV(Unknown Source) 
    at com.insparq.jobserver.test.ScheduledJobTest.processJob(ScheduledJobTest.java:173) 
    at com.insparq.jobserver.test.ScheduledJobTest.test(ScheduledJobTest.java:111) 
    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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
    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:164) 
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172) 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70) 
Caused by: org.postgresql.util.PSQLException: Bad value for type timestamp : 
    at org.postgresql.jdbc2.TimestampUtils.loadCalendar(TimestampUtils.java:246) 
    at org.postgresql.jdbc2.TimestampUtils.toTimestamp(TimestampUtils.java:321) 
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getTimestamp(AbstractJdbc2ResultSet.java:437) 
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getTimestamp(AbstractJdbc2ResultSet.java:2446) 
    at org.apache.commons.dbcp.DelegatingResultSet.getTimestamp(DelegatingResultSet.java:229) 
    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.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:104) 
    ... 82 more 
Caused by: java.lang.NumberFormatException: Timestamp has neither date nor time 
    at org.postgresql.jdbc2.TimestampUtils.loadCalendar(TimestampUtils.java:243) 
    ... 91 more 

有沒有辦法告訴Hibernate只加載導致它變得不填充空值的元素休息?

回答

0

Hibernate沒有固定的列表大小,它只是執行查詢。 它不能將行值轉換爲時間戳。可能是行有日期類型。 當我在數據庫中設置了非空限制之後,當行包含零值(00/00/00 ...)時,我在mysql中出現了類似的錯誤。

+0

我不知道是怎麼回事上,但看起來像是司機的問題。我更新到9.2版,但仍然出現錯誤。當我運行簡單選擇它返回數據罰款。所以只是爲了它,我已經將列從非null更改爲null,然後將created_at設置爲null。一切都很順利。然後我使用to_timestamp函數更新了created_at。它開始再次失敗。我完全迷失在這裏。 – Mike

0

我不認爲isNotEmpty是像createdAt這樣的時間戳字段的有效限制。

根據文檔,它僅適用於收集財產。

約束集合值屬性爲非空

你應該在這裏使用isNotNull

public List<Products> getProductsListByIds(List<String> ids, Integer storeId) { 
    Criteria criteria = getSession().createCriteria(Products.class) 
     .add(Restrictions.eq("storeId", storeId)) 
     .add(Restrictions.in("productId", ids)) 
     .add(Restrictions.isNotNull("createdAt")) 
     .add(Restrictions.isNotNull("updatedAt")) 
     .add(Restrictions.isNotNull("id")) 
    return criteria.list(); 
} 

另一個側面說明:Postgres的驅動程序9.1.901似乎有些系列的性能問題,我建議移動到9.1.902最新版本或9.1.903