方法Hibernate.createBlob()从Hibernate 4.0.1中弃用,并移至Hibernate.getLobCreator(Session session).createBlob() [英] Method Hibernate.createBlob() is deprecated from Hibernate 4.0.1 and moved to Hibernate.getLobCreator(Session session).createBlob()
问题描述
方法Hibernate.createBlob()
,并将其移至了Hibernate.getLobCreator(Session session).createBlob()
.任何解决方案都应在方法getLobCreator(Session session)
中传递什么,即代替Session,或者其他解决方案,展示如何使用Spring和Hibernate将图像检索并保存到DB中.
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.
推荐答案
根据此简单教程,
会话对象
会话用于获得与数据库的物理连接. Session对象是轻量级的,旨在 每次需要与数据库进行交互时都实例化. 永久对象通过会话对象保存和检索.
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.
在Hibernate 4.0+中,您可以从SessionFactory
获取Session
对象.让我们为此任务编写一个方便的类.
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();
}
}
然后:
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.
或(假设Employee
是具有字段@Lob private byte[] photo;
的POJO,绑定到相应的表):
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();
来自 mkyong.com 的信息.在这里,您可以找到有关如何将图像保存到数据库的完整示例.还有如何检索图像的示例.
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.
注意:对于Hibernate 4.3及更高版本,您在try
块中的代码会稍有变化.因为类ServiceRegistryBuilder
被StandardServiceRegistryBuilder
代替.
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());
这篇关于方法Hibernate.createBlob()从Hibernate 4.0.1中弃用,并移至Hibernate.getLobCreator(Session session).createBlob()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!