3

Our project needs to support both Oracle and Postgres Dbs. And there may be more databases added to this list. So need a DB agnostic Hibernate configuration for BLOB and CLOB datatypes.

While Oracle was working fine with:

@Lob
@Column(name="column1")
private String str;

@Lob
@Column(name="column2")
private byte[] bytea;

Postgres started complaining with the same.

org.springframework.orm.hibernate4.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [insert into TABLE_NAME (TABLE_TYPE, TABLE_ID, TABLE_CONTENT_BIN, TABLE_CONTENT_CHAR) values (?, ?, ?, ?)]; SQL state [0A000]; error code [0]; could not insert: [com.project.EntityClass]; nested exception is org.hibernate.exception.GenericJDBCException: could not insert: [com.project.EntityClass]] Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [com.project.EntityClass] at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3144) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258) at org.springframework.orm.hibernate4.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:104) ... 40 more Caused by: java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4PreparedStatement.setCharacterStream(int, Reader, long) is not yet implemented. at org.postgresql.Driver.notImplemented(Driver.java:670) at org.postgresql.jdbc4.AbstractJdbc4Statement.setCharacterStream(AbstractJdbc4Statement.java:116) at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$4$1.doBind(ClobTypeDescriptor.java:124) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281) at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2857) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121) ... 48 more

Researched and found that people are suggesting annotating with @Type( type = "org.hibernate.type.StringClobType" ) and @Type( type = "org.hibernate.type.BinaryType" ). Both of which are Hibernate Annotations. However, we intend to use JPA annotations and this would need Hibernate at runtime, which seems to me as unnecessary.

So my question is if there is a way to handle BLOBs and CLOBs in a very generic way, which would not require us to branch codes for different DBs and without adding Hibernate specific annotations. Also, we do not use hibernate for table creation.

Postgres version- 9.4-1201-jdbc41

Oracle Version - 12.1.0.1

Hibernate Version -4.3.10.Final

2
  • With other JPA providers you do as you have there. Perhaps when you say something is "complaining", maybe better to mention WHAT message + stack trace Commented Aug 1, 2016 at 15:04
  • @NeilStockton: Added message and stack trace. Please help Commented Aug 3, 2016 at 11:10

1 Answer 1

3

Since I got no other answers from here, I thought I will post what I have done to solve my problem, which may help others.

Since we were not creating tables with Hibernate, I read somewhere that hibernate/jpa would be able to convert the fields into appropriate types without the @Lob annotation. Ex.- for postgres into text and for oracle into BLOB for a field of type byte[].

Tried that and worked for both Postgres and Oracle. :)

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

2 Comments

Awesome, two years old but still valid.
In 2022 is still the simplest solution I found.

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.