-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Write to a blob field via OutputStream
PostPosted: Tue Feb 03, 2015 1:17 pm 
Newbie

Joined: Tue Jan 31, 2006 4:59 am
Posts: 4
I'm looking for a way to directly write a blob on an entity using an OutputStream. I don't know the size of the data in advance, so I need the OutputStream way of doing it.
I've already tried:
Code:
    @Lob
    @Column(length = 10 * 1014 * 1024)
    @Basic(fetch = FetchType.LAZY)
    private Blob document;

    public OutputStream getDocumentOutputStream(EntityManager entityManager) throws SQLException {
        HibernateEntityManager hibernateEntityManager = entityManager.unwrap(HibernateEntityManager.class);
        Session session = hibernateEntityManager.getSession();
        LobHelper lobHelper = session.getLobHelper();
        this.document = lobHelper.createBlob(new byte[0]);
        session.save(this);
        session.flush();
        session.refresh(this);
        return this.document.setBinaryStream(1);
    }

Writing to the OutputStream I get from getDocumentOutputStream(entityManager) works on H2 and MySQL, but not from within JBoss EAP 6.3.2 using MySQL (Arquillian integration test). Is this a bug in that version of Hibernate?


Top
 Profile  
 
 Post subject: Re: Write to a blob field via OutputStream
PostPosted: Tue Feb 03, 2015 3:30 pm 
Newbie

Joined: Tue Jan 31, 2006 4:59 am
Posts: 4
I've found the solution. After writing to the OutputStream you have to do
Code:
entityManager.merge(theEntity)

A clean way to ensure this is to wrap the OutputStream and override close() to invoke the merge automatically.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.