Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate expected NUMBER got BINARY on null field

Tags:

java

hibernate

I can't understand why Hibernate is trying to set this field as a VARBINARY when it is null.

The Java data type is BigDecimal The Oracle data type is Float

It is being set like this:

entityManager.createNamedQuery("blah").setParameter("target_field", object.sourceValue)

again - sourceValue is a BigDecimal, and when i debug it the value is null.

when it tries to execute this update, I get oracle error:

ORA-00932: inconsistent datatypes: expected NUMBER got BINARY

this is what shows up for this property in the console log:

o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [VARBINARY] - [null]

IF I do this silly hack in Java before I execute the update:

if (object.sourceValue == null) object.sourceValue = new java.math.BigDecimal(0);

Then it runs fine. So the cause of this error is definitely not anything else than hibernate doing something wrong when the field is null.

How do I fix this so I can set the field to null without hibernate mishandling it?

In the DB, the field is nullable.

like image 856
Trant Avatar asked May 20 '16 00:05

Trant


2 Answers

It looks the issue has not been resolved yet even with newer version of hibernate 5.2.17.Final with JPA.

The null parameter is being sent as VARBINARY which causes the error

ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - ERROR: cannot cast type bytea to bigint
like image 60
Houmam Avatar answered Oct 05 '22 22:10

Houmam


One workaround to this is to use the org.hibernate.Query that is wrapped into the org.hibernate.ejb.QueryImpl this way:

QueryImpl q = (QueryImpl) this.entityManager.createNamedQuery("myNamed.query");
q.getHibernateQuery().setParameter("name", value, org.hibernate.Hibernate.BIG_DECIMAL);
q.getResultList();

When value is null, hibernate still can know which type the parameter must be, so it doesn't translate the null to a binary value.

like image 27
Emiliano Schiano Avatar answered Oct 05 '22 23:10

Emiliano Schiano