Java 方法 Hibernate.createBlob() 已从 Hibernate 4.0.1 弃用并移至 Hibernate.getLobCreator(Session session).createBlob()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18051290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:05:03  来源:igfitidea点击:

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

javahibernate

提问by Ankit Tater

Method Hibernate.createBlob()is deprecated from Hibernate 4.0.1and 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.

方法Hibernate.createBlob()已从Hibernate 4.0.1弃用并移至Hibernate.getLobCreator(Session session).createBlob(). 任何解决方案我应该在方法内部传递什么getLobCreator(Session session),即代替会话,或者任何其他解决方案,展示如何使用 Spring 和 Hibernate 检索图像并将其保存到数据库中。

回答by naXa

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.

会话对象

会话用于获取与数据库的物理连接。Session 对象是轻量级的,旨在在每次需要与数据库进行交互时进行实例化。 持久对象通过 Session 对象进行保存和检索

会话对象不应长时间保持打开状态,因为它们通常不是线程安全的,应根据需要创建和销毁它们。

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

在 Hibernate 4.0+ 中,您可以SessionSessionFactory. 让我们为这个任务编写一个方便的类。

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 Employeeis a POJO with a field @Lob private byte[] photo;, binded to the corresponding table):

或者(假设Employee是一个带有字段的 POJO @Lob private byte[] photo;,绑定到相应的表):

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.

来自mkyong.com 的信息。在这里您可以找到如何将图像保存到数据库的完整示例。以及如何检索图像的示例。



Note: For Hibernate 4.3+ your code inside tryblock slightly changes. Because class ServiceRegistryBuilderis replaced by StandardServiceRegistryBuilder.

注意:对于 Hibernate 4.3+,try块内的代码略有变化。因为 classServiceRegistryBuilder被替换为StandardServiceRegistryBuilder.

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());