1

I have two tables (A and B): one of table is structure of some object (A) and other table that connected two tables A and table C by many-to-many mapping (B). In table A I have column that must be computed using table B. How can I make this column auto-computing in hibernate?

Main question: how to make column in Hibernate as query?

Upd:

I have tried @Formula annotation and have got org.hibernate.type.SerializationException: could not deserialize.

Do you have some idea about this situation?

The code of this field looks like:

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

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

Stack trace:

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 Answers 2

1

You can use @Formula annotation and define an Entity field based on computed SQL expression.

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

where id is column of table where the Entity is mapped

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, but I have tried it and have got org.hibernate.type.SerializationException: could not deserialize. Do you have some idea about this situation? The code I put into question.
could be issue with setting annotation on fields and methods stackoverflow.com/questions/13253403/…
1

Haven't tried it myself but this thread in the hibernate forums pretty much describes the same problem you are encountering with the @Formula annotation.

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

According to the thread'S author changing the computed field's type from int to Long solved the deserialization issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.