我使用 Spring 4.2.3、Hibernate 5.0.4、JPA 2.1 进行 ORM self 训练。今天我正在学习如何存储Blob在 Oracle 11.2.0.4 数据库中。
在我的应用程序中,我有 Service 层和 Dao 层。
问题: 我为什么要创建Blob实例使用 Hibernate.getLobCreator哪个需要当前会话?为什么无法再使用 [Hibernate.createBlob][3](无需访问当前 session )?
在某些情况下,我可能需要带有 Blob 的 transient 域模型字段初始化,无需使用 Dao 将其持久保存在数据库中。
是因为(如 Blob JavaDoc 中所写):
A Blob object is valid for the duration of the transaction in which is was created.
很抱歉问了愚蠢的问题,但我无法找到满意的答案。
最佳答案
因为由于某些增强功能,Hibernate.createBlob 现在已从 hibernate 中删除(我不记得确切的 jira 问题:)。您可以在互联网上找到)。
因此,您可以使用新的 Hibernate.getLobCreator,如下所示:
public Blob createBlob(your param) {
return Hibernate.getLobCreator(currentSession()).createBlob();
}
// check different method for createBlob as you want to pass params.
// currentSession is method of HibernateDaoSupport
// pass your session object instead of above
这里的Blob是java.sql.Blob的类型。不同类的其他 blob 方法正在实现此接口(interface)。因此,无论您使用的是 oracle 还是 sql server 或任何其他东西,您都会获得正确的 Blob...
因此,如果您使用 Hibernate 5.x.x,请像上面那样使用。
关于java - 使用 Hibernate 和 Spring 创建 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34766338/