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();

Leon

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:
Hello

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 (res.next()) {
          BLOB oracleBLOB = res.getBLOB(1);
          OutputStream out = oracleBLOB.setBinaryStream(0L);
          streamer.stream(out);
          out.close();

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

Leon



-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink , http://wiki.oracle.com/page/TopLink TopLink Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , http://www.nabble.com/EclipseLink-f26430.html EclipseLink Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence



Back to the top