Question: If entity.getHistory() is null following code snippet:

(getEntityManager() returns spring injected EntityManager, database field history type is: text or varchar2(2000)

```
Query query = getEntityManager().createNativeQuery("insert into table_name(..., history, ....) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
[...]
.setParameter(6, entity.getHistory())
[...]

query.executeUpdate();

```

Gives strange exception:

```
17/11/11 06:26:09:009 [pool-2-thread-1]  WARN util.JDBCExceptionReporter:100 - SQL Error: 0, SQLState: 42804
17/11/11 06:26:09:009 [pool-2-thread-1] ERROR util.JDBCExceptionReporter:101 - ERROR: **column "history" is of type text but expression is of type bytea**

```

Hint: You will need to rewrite or cast the expression.

Problem occurred only in this configuration:  

OS: CentOS release 5.6 (Final)  

Java: 1.6.0\_26  

DB: PostgreSQL 8.1  

JDBC Driver: postgresql-9.1-901.jdbc4  

Application Server: apache-tomcat-6.0.28

Everything is working fine on few other configurations or when history is empty string.
Same statement executed from pgAdmin works fine.

I guess problem is in PostgreSQL JDBC driver, is there some sensible reason to treat null string as a bytea value? Maybe some strange changes between Postgres 8 and 9?

Answer: 
Using Hibernate specific Session API works as a workaround:

```
String sql = "INSERT INTO person (id, name) VALUES (:id, :name)";
Session session = em.unwrap(Session.class);
SQLQuery insert = session.createSQLQuery(sql);
sql.setInteger("id", 123);
sql.setString("name", null);
insert.executeUpdate();

```

I've also filed [HHH-9165](https://hibernate.atlassian.net/browse/HHH-9165) to report this issue, if it is indeed a bug.
I had the same problem executing an UPDATE with Oracle Database.
Sometimes a few fields that are updated are null and this issue occurs. Apparently JPA is casting my null value into bytea as already spoke in this topic.

I give up of using the Query object to update my table and used the EntityManager.merge(Entity) method.

I simply create the Entity object with all informations and when I execute the merge(Entity), the JPA executes a SELECT, compares the information already persisted on the Data Base with my Entity object and executes the UPDATE correctly. That is, JPA create a "field = NULL" sentence for the Entity variables that are null.