如何在数据库(hibernate)中计算表值?

时间:2016-03-11 09:31:12

标签: java mysql sql hibernate postgresql

我有两个表(A和B):一个表是某个对象(A)的结构,另一个表是通过多对多映射(B)连接两个表A和表C.在表A中,我有必须使用表B计算的列。如何在hibernate中使该列自动计算?

主要问题:如何在Hibernate中创建列作为查询?

UPD:

我已尝试@Formula注释并获得了org.hibernate.type.SerializationException: could not deserialize

你对这种情况有所了解吗?

此字段的代码如下所示:

@Formula("(select count(t.first_id) from " + TABLE_NAME + " as t group by t.second_id)")
private int count;

setCount(...);
... getCount();

堆栈追踪:

org.hibernate.type.SerializationException: could not deserialize
        at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
        at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
        at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:53)
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:241)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:237)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:227)
        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:303)
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2726)
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1728)
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1654)
        at org.hibernate.loader.Loader.getRow(Loader.java:1543)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
        at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
        at org.hibernate.loader.Loader.doQuery(Loader.java:930)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
        at org.hibernate.loader.Loader.doList(Loader.java:2611)
        at org.hibernate.loader.Loader.doList(Loader.java:2594)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
        at org.hibernate.loader.Loader.list(Loader.java:2418)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1716)
        at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
        at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:385)

2 个答案:

答案 0 :(得分:1)

您可以使用@Formula注释并根据计算的SQL表达式定义实体字段。

@Formula(" select count(*) from C where C.dependent_id=id")
public int expressionCount;

其中id是实体映射的表的列

答案 1 :(得分:1)

我自己没试过,但是hibernate论坛中的这个帖子几乎描述了你遇到@Formula注释时遇到的同样问题。

https://forum.hibernate.org/viewtopic.php?f=1&t=1010737&view=previous

根据线程'作者将计算字段的类型从int更改为Long解决了反序列化问题。