Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Hibernate.createBlob() is deprecated from Hibernate 4.0.1 and moved to Hibernate.getLobCreator(Session session).createBlob()

Tags:

java

hibernate

Method Hibernate.createBlob() is deprecated from Hibernate 4.0.1 and moved to Hibernate.getLobCreator(Session session).createBlob(). Any solution what should I pass inside method getLobCreator(Session session), i.e in place of Session, Or any other solution showing how to retrieve and save an image into DB using Spring and Hibernate.

like image 265
Ankit Tater Avatar asked Aug 05 '13 05:08

Ankit Tater


1 Answers

According to this easy tutorial,

Session Object

A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed.

In Hibernate 4.0+ you can get Session object from a SessionFactory. Let's write a handy class for this task.

package your.company.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration().configure();
            ServiceRegistry registry = new ServiceRegistryBuilder()
                    .applySettings(configuration.getProperties())
                    .buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(registry);
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

Then:

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();

byte[] bFile = /* load image into byte array */;
Blob image = Hibernate.getLobCreator(session).createBlob(bFile);
/* ? Your actions with Blob ? */

session.getTransaction().commit();

Let me know, if it works.

Or (assume Employee is a POJO with a field @Lob private byte[] photo;, binded to the corresponding table):

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();

byte[] bFile = /* load image into byte array */;
Employee employee = new Employee();
employee.setPhoto(bFile);

session.save(employee);

session.getTransaction().commit();

Info from mkyong.com. Here you can find the full example of how to save image into database. And the example of how to retrieve image.


Note: For Hibernate 4.3+ your code inside try block slightly changes. Because class ServiceRegistryBuilder is replaced by StandardServiceRegistryBuilder.

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
like image 191
naXa Avatar answered Oct 22 '22 14:10

naXa