I am trying to insert a record into a table which has column of type clob. Now I am using hibernate and persistent manager. The code is:
<code> this.persistenceManager.saveOrUpdate(myDetails); </code> The myDetails is instance of POJO class for table Details which has one column of type clob.
Now my question is how do I set clob value in this? I searched for this and I found following code <code> public CLOB stringToClobForOracle(String result) { CLOB tempClob = null; try { Class.forName(driver); Connection conn = DriverManager.getConnection(connectionString, userName, password); tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION); tempClob.open(CLOB.MODE_READWRITE); // Get the output stream to write Writer tempClobWriter = tempClob.getCharacterOutputStream(); // Write the data into the temporary CLOB if (result != null && !result.isEmpty()) { tempClobWriter.write(result); } else { tempClobWriter.write(" "); }
// Flush and close the stream tempClobWriter.flush(); tempClobWriter.close();
// Close the temporary CLOB tempClob.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return tempClob; } </code>
Is there any better way of doing it? I am using CLOB (instance of oracle.sql.CLOB). Instead can I use something generic. That's the first question.
Now the second error. I am doing this generation of clob much before I am calling saveOrUpdate(). So I am getting error
"Caused by: java.sql.SQLException: ORA-22922: nonexistent LOB value" at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743) at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216) at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1168) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3316) at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3400) at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:365) at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:665) at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:740) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
Not sure why its saying Blob and not clob. I searched for this and I got the reason: “It’s the problem of while creating one BLOB object we are using one connection and closing it and for insertion statement we are using another connection.”
How do I do it on one connection because I am using generic method to insert record saveOrUpdate which I cannot modify. <code> public <T> void saveOrUpdate(final T entity) { getHibernateTemplate().saveOrUpdate(entity); } </code>
How do I now generate and save clob in one connection? Kindly let me know what alternatives I have.
Thanks in advance.
|