Hibernate - Out of memory inserting big blob

左心房为你撑大大i 提交于 2019-12-03 16:47:01

I believe that the problem is that your in-memory object still has it's byte array populated. This is a separate issue from whether or not a stream is used to persist it to the database.

I suggest a work-around: don't store the blob in the POJO. Store the blob separately, either in the database via straight JDBC, or on disk, and then just keep a reference to the blob in your POJO (either the database primary key or the disk path/filename).

Then, when you need the blob back, get the reference from the POJO and use stream-based methods to get it back.

It's more work to get / save the blob every time that you need to, but if the blob is too large for memory, you may not have any other choice.

I dont know why it's not working and it it should, but that what i have done to save blob to database using Hibernate:

in POJO mapped to a table:

    private byte[] serializedDocx; // + getter and setter

in a method:

    ...
     SessionFactory mf = ...
     sess = mf.openSession();
     //open connection
     sess.beginTransaction();
    ...
      ByteArrayOutputStream docxBos = new ByteArrayOutputStream();
      [write to docxBos]
      someEntity.setDocx(docxBos.toByteArray());
      sess.save(someEntity);

in myEntity.hbm.xml

  <property name="serializedDocx" type="binary">
      <column name="serialized_docx" not-null="true"/>
    </property>

in MySQL:

CREATE TABLE IF NOT EXISTS `docx` (
  `serialized_docx` longblob NOT NULL, ...

HTH.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!