gpt4 book ai didi

当总和对于字段类型而言太大时,来自 Projections.sum 的 Hibernate 错误

转载 作者:行者123 更新时间:2023-12-03 07:43:36 24 4
gpt4 key购买 nike

我正在尝试使用投影来总结按角色分组时的大小字段。标准工作正常,除非总和不再适合大小字段的类型。当我直接对数据库运行生成的查询时,我使用 MySQL 的数据库返回了一个更大的总和类型,但我从 Hibernate 中得到了异常。大小字段在 Java 中是 long,在 MySQL 中是 BIGINT。无论如何,有没有办法让 Hibernate 返回更大的类型,它似乎试图将总和强制转换为要求和的字段的类型,即使数据库以更大的类型返回它。

The projection portion of the criteria looks like this:
List<Object[]> roleSummaries = session.createCriteria(PhysicalDisk.class)
.setProjection(Projections.projectionList()
.add(Projections.groupProperty(PhysicalDisk_.role), "role")
.add(Projections.rowCount(), "count")
.add(Projections.sum(PhysicalDisk_.totalBytes), "space")
)
.createCriteria(PhysicalDisk_.raidGroup, "raidGroup")
.createCriteria("raidGroup." + RAIDGroup_.plex, "plex")
.add(Restrictions.eq("plex." + Plex_.diskAggregate, diskAggregate))
.list();

我得到的异常(exception)是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '12000010002048860160' in column '3' is outside valid range for the datatype BIGINT.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_21]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_21]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_21]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_21]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.Util.getInstance(Util.java:384) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.ResultSetImpl.throwRangeException(ResultSetImpl.java:7970) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.ResultSetImpl.parseLongAsDouble(ResultSetImpl.java:7254) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2944) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2909) ~[mysql-connector-java.jar:na]
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:3021) ~[mysql-connector-java.jar:na]
at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:278) ~[commons-dbcp.jar:1.4]
at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:278) ~[commons-dbcp.jar:1.4]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:61) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:148) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:639) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:829) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2533) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.Loader.list(Loader.java:2271) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716) ~[hibernate-core.jar:3.6.0.Final]
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347) ~[hibernate-core.jar:3.6.0.Final]
at com.netapp.dfm.entity.storage.WAFLDiskEntityManager.findDiskPhysicalSummaryForCluster(WAFLDiskEntityManager.java:153) ~[dfm-data-access.jar/:na]
at com.netapp.dfm.entity.storage.WAFLDiskEntityManager$$FastClassByCGLIB$$707d513f.invoke(<generated>) ~[cglib-nodep.jar:na]
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) ~[cglib-nodep.jar:na]
...

最佳答案

注意:未经测试

Projections.sum() 的实现如下:

return new AggregateProjection("sum", propertyName);

并且AggregateProjection使用属性的类型作为函数返回的类型(sum)

您可以改用 AggregateProjection 的自定义子类,其中重写 getTypes 方法以返回 new Type[] {BigIntegerType.INSTANCE}而不是求和属性的类型。

关于当总和对于字段类型而言太大时,来自 Projections.sum 的 Hibernate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7114081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com