Java – hibernate on Oracle: mapping string attributes to CLOB columns
Warning: please see my own answer below The problem is caused by the addition of 10.2 Caused by an old Oracle driver in a classpath other than 0.4 The problem is solved Leave the rest of the problem for future generations
I've been hitting the following things This is a simple POJO removed from my application code:
@Entity
@Table(name = "PIGGIES")
public class Piggy {
private Long id;
private String description;
public Piggy() {}
@Id
@GeneratedValue
@Column(name = "PIGGY_ID")
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
@Lob
@Column(name = "PIGGY_DESCRIPTION")
public String getDescription() { return description; }
public void setDescription(String d) { description = d; }
}
There is a string attribute and a CLOB column When the content is short (such as "Hello world"), it is still good Using a long string, I get the following exception:
java.sql.sqlException: operation not allowed: streams type cannot be used in batching
at oracle.jdbc.dbaccess.DBError.throwsqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwsqlException(DBError.java:179)
at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:4236)
at org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)
at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2403)
I use hibernate 3.2 3 and Oracle jdbc driver 10.2 0.4. An exception message indicates that the batch may have failed Although I can disable batching in this simple case, I need to enable it for "real" POJOs In fact, now it seems that query batch processing is the only reason why we use hibernate
So my question is, how can I do the above work?
Edit: interesting observation: as long as it is 1333 characters long, the value of my "description" attribute is still very good So strange!
Edit 2: in order to find a solution, I modified getproperty () with the following comments, which makes no difference:
@Lob
@Type(type="text")
@Column(name = "PIGGY_DESCRIPTION",length = Integer.MAX_VALUE)
public String getDescription() { return description; }
Edit 3: DDL of "piggies":
CREATE TABLE "PIGGIES"
( "PIGGY_ID" NUMBER NOT NULL ENABLE,"PIGGY_DESCRIPTION" CLOB,CONSTRAINT "PIGGIES_PK" PRIMARY KEY ("PIGGY_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 1048576 NEXT 1048576 mineXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "BBDATA" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 1048576 NEXT 1048576 mineXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "BBDATA"
LOB ("PIGGY_DESCRIPTION") STORE AS "SYS_LOB0000177753C00002$$"(
TABLESPACE "BBDATA" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE
STORAGE(INITIAL 1048576 NEXT 1048576 mineXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)) ;
This is the entire stack:
org.hibernate.exception.GenericJDBCException: Could not update: [com.bamnetworks.cms.types.Piggy#934]
at org.hibernate.exception.sqlStateConverter.handledNonSpecificException(sqlStateConverter.java:103)
at org.hibernate.exception.sqlStateConverter.convert(sqlStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2425)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
Caused by: java.sql.sqlException: operation not allowed: streams type cannot be used in batching
at oracle.jdbc.dbaccess.DBError.throwsqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwsqlException(DBError.java:179)
at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:4236)
at org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:172)
at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2403)
... 45 more
Solution
Moron Alert: it turns out that I have an old jar and nine Oracle JDBC classes on my classpath After cleaning up, everything just works magically with the following comments:
@Lob
@Column(name = "PIGGY_DESCRIPTION")
public String getDescription() { return description; }
Blame fat fingers
