Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Write BLOB using EntityManager

Hi James,

I have not mapped on object to the table, so I did it by getting the connection from the EntityManager to run the JDBC code below.

UnitOfWorkImpl uow = (UnitOfWorkImpl)((JpaEntityManager)getEntityManager()).getActiveSession().acquireUnitOfWork();
Connection conn = uow.getAccessor().getConnection();


James Sutherland wrote:
If you have an object mapped to the table, you could try just reading and
updating the entire blob as a byte[] in the object.  If you use the
Oracle9Platform we will stream the blob to get around the 4k size limit.

Otherwise to stream the blob yourself is pretty low-level JDBC, you may need
to continue using JDBC code for this.

Leon Derks-2 wrote:

I have a question, how can I write data to a blob, using the EntityManager.createNativeQuery("select image_data from image_blobs where image_id = ? and image_type = ? for update nowait")?
How can I get the stream from the blob?

I used to do this as follows:
PreparedStatement stmt = conn.prepareStatement("select image_data from image_blobs where image_id = ? and image_type = ? for update nowait");
        stmt.setLong(1, image.getId().longValue());
        stmt.setString(2, image.getType());
        res = (OracleResultSet) stmt.executeQuery();

        // get the stream from the blob and let data be streamed into it
        if ( {
          BLOB oracleBLOB = res.getBLOB(1);
          OutputStream out = oracleBLOB.setBinaryStream(0L);

          // now determin the size
          stmt = conn.prepareStatement(GET_SIZE);
          stmt.setLong(1, image.getId().longValue());
          stmt.setString(2, sizeName);
          res = (OracleResultSet) stmt.executeQuery();
          if ( {
            size = res.getLong(1);


--- James Sutherland
 EclipseLink ,
TopLink Wiki: EclipseLink , TopLink Forums: TopLink , EclipseLink Book: Java Persistence

Back to the top