7

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.

Trant
  • 3,461
  • 6
  • 35
  • 57
  • see http://stackoverflow.com/questions/2123438/hibernate-how-to-set-null-query-parameter-value-with-hql – Scary Wombat May 20 '16 at 00:33
  • 1
    or http://stackoverflow.com/questions/25667914/how-to-set-integer-parameter-as-null-in-hibernate-query – Scary Wombat May 20 '16 at 00:41
  • 1
    The first link has to do with where clauses, but the second link does apply. The problem is in their solution they talk about using some setParameter (string, object, type) method, which does not exist unless it's available only in a newer hibernate version than I am forced to use (4.3.11) – Trant May 20 '16 at 01:09
  • Yeah, that confused me as well. My takeaway from the link was just to create two separate queries. – Scary Wombat May 20 '16 at 01:11
  • Hibernate 4.3.11 supports use of JPA 2.0 so you should be able to use the typed parameters – Zilvinas May 20 '16 at 01:53

3 Answers3

4

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
Houmam
  • 567
  • 4
  • 11
1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Emiliano Schiano
  • 1,862
  • 1
  • 25
  • 30
1

I have solved this problem by adding 2 annotations

@DynamicUpdate(value=true)
@DynamicInsert(value=true)

Also and most important, check mapping on your entity class for getters like (onetomany, manytoone). All the best.

Igor F.
  • 2,649
  • 2
  • 31
  • 39