1

I have Inputstream/byte[] of an image file, which I want to store in DB, but I already stored the entity only I want to update the blob field, I have Id of the row. I tried following but it is not working for me:

Query query = session.createSQLQuery( "update tableName set blobFieldName=:blbContent where id=5);

query.setBinary("blbContent",myByteArray)

int val = query.executeUpdate();

n this val returned 1, but actually in DB there is no updation happened...I can't understand where I'm wrong. Please help me. I need this working...

3
  • 1
    2 things: a) you can't stream in byte data in a DML statement without encoding it b) your statement has "blobFieldName" - is that where the blob is stored? You should be using the hibernate session to persist the object. Commented Jun 1, 2012 at 13:07
  • 1
    Many databases do not allow direct manipulation of LOB data in this manner. That said, if your database does not it should be throwing an SQLException. If the statement "succeeds" then I would have to think you are having a different problem. Did you commit transaction? Commented Oct 2, 2012 at 14:46
  • yeah...I just forgot to commit the transaction..Its working now.. Commented Oct 3, 2012 at 5:55

1 Answer 1

1

I encountered the same problem, but I found a solution.

I'll share my code with you:

//this is a update method
**public boolean updateImage(Image image) { 

    Session session = template.getSessionFactory().openSession();

    Transaction transaction = session.beginTransaction();

    session.saveOrUpdate(image);

    session.flush();

    transaction.commit();

    session.close();

    return true;
}**

the below is my Image class, I use the image type is Blob

public class Image {


    private int id;

    private Blob image;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Blob getImage() {
        return image;
    }

    public void setImage(Blob image) {
        this.image = image;
    }
}

Hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.