Note that there are some explanatory texts on larger screens.

plurals
  1. POSave blob using a stream from EJB to database (in a memory efficient way)
    primarykey
    data
    text
    <p>I want to save large attachments (500Mb, maybe, if possible, even > 2Gb) in a database. I know the pros and cons of doing so are discussed very frequently, but that's not the focus of my question.</p> <p>The conventional way of storing Blob fields in EJB3 with JPA is to use a code as following:</p> <pre><code>@Lob private byte[] data; </code></pre> <p>This becomes a problem when handling huge data sources, because the whole byte array is saved in memory.</p> <p>I tried to change it to Blob:</p> <pre><code>@Lob private Blob data; </code></pre> <p>But this results in the same problem. When calling</p> <pre><code>// session: Hibernate session. // Actually, I'd like to stay with JPA's abstraction and not // reference Hibernate directly, if possible Blob data = session.createBlob(inputStream, lengthOfStream); </code></pre> <p>the createBlob method creates a byteArray from the inputStream.</p> <p>Because of the ORM mapping, I also wonder how to handle the insertion of the data. One idea was to make an entity variable</p> <pre><code>@Lob private byte[] data; </code></pre> <p>That I will never use. This way, the database schema get's build. But because the @Lob annotation is lazy, it will not bloat my memory.</p> <p>And then write</p> <pre><code>entityManager.persist(dataObject); // The following lines are _completely_ imaginatory!! query.prepare("update DataObject d set d.data = :dataInputStream where d = :dataObject"); query.setProperty("dataObject", dataObject); query.setProperty("dataInputStream", someDataInputStream); </code></pre> <p>One solution I stumbled across looks nice, but doesn't use JPA: <a href="https://stackoverflow.com/questions/324104/grooviest-way-to-store-a-blob-in-a-database">Grooviest way to store a BLOB in a database?</a></p> <p>Does anybody have an advice on how to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload