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: 
Since you're calling `setParameter(int,Object)` with a null value, at a guess the entity manager has no idea which persistence type to use to bind the parameter. Istr Hibernate having a predilection for using SerializableType, which would equate to a bytea.

Can you use the setParameter method that takes a Parameter object instead, so you can specify the type? I haven't really used JPA, so can't give a detailed pointer.

(IMHO this is your just desserts for abusing EntityManager's native-sql query interface to do inserts, rather than actually mapping the entity, or just dropping through to JDBC)
If you are willing to use the [PreparedStatement](http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html) class instead of Query:

```
if (entity.getHistory() == null)
    stmt.setNull(6, Types.VARCHAR);
else
    stmt.setString(6, entity.getHistory());

```

(It is possible that using `?::text` in your query string would also work, but I've never done it that way myself.)