milind_db
milind_db

Reputation: 1294

How to update blob field using simple hibernate query

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...

Upvotes: 1

Views: 3207

Answers (1)

Ziv
Ziv

Reputation: 113

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.

Upvotes: 1

Related Questions