java - 为什么不推荐使用 buildSessionFactory()?

标签 java hibernate

为什么 buildSessionFactory() 被替换为 buildSessionFactory(ServiceRegistry)? ServiceRegistry 的重要性是什么?

最佳答案

Hibernate的Jira上解释了这个原因

https://hibernate.onjira.com/browse/HHH-2578

Currently a SessionFactory is built by throwing a bunch of stuff into a Configuration object, stirring it, letting it come to a boil, and then pulling out the SessionFactory. In seriousness, there are a few problems with the way we currently operate within a Configuration and how we use it to build a SessionFactory: The general issue that there is no "lifecycle" to when various pieces of information will be available. This is an important omission in a number of ways: 1) consider schema generation. currently we cannot even know the dialect when a lot of db object names are being determined. this would be nice because it would allow us to transparently handle table/column names which are also keywords/reserved-words in the dialect, for example. 2) static-ness of types and the type-mappings. Because we currently have nothing to which to scope them. Ideally a type instance would be aware of the SessionFactory to which it is bound. Instead, what we have now is to change API methods quite a lot of the time to add in the SessionFactory as a passed parameter whenever it is discovered that it is needed. 3) also, most (all?) of the "static" configuration parameters in Hibernate are currently required to be so because of their use from within these static types; thus scoping types would allow us to also scope those config parameters (things like bytecode-provider, use of binary streams, etc). Ideally what I see happening is a scheme where users build a org.hibernate.cfg.Settings (or something similiar) instance themselves. Additionally they would apply metadata to a registry of some sort (lets call it MetadataRegistry for now). Then in order to build a SessionFactory, they would supply these two pieces of information (via ctor? via builder?). The important aspect though is that the information in MetadataRegistry would not be dealt with until that point in time, which would allow us to guarentee that resolving schema object names, types, etc would have access to the runtime Settings (and specifically the dialect)

您还可以阅读对此的评论:https://hibernate.onjira.com/browse/HHH-7580 复制粘贴太多了,我想 Jira 不会下降,所以这个答案应该是有效的。

关于java - 为什么不推荐使用 buildSessionFactory()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579301/

相关文章:

java - 如何绘制如下图所示的形状

java - 将图像插入布局,然后用于电子邮件应用程序

java - 是否可以在单独的类/文件中映射 Hibernate 的普通 Java bean?

java - 如何以编程方式将映射资源添加到 hibernate 配置以创建 session 工厂?

java - Spring Data/Hibernate @ManyToOne 在不应该的情况下自动级联

java - 为什么用 simpledateformat 解析会抛出空指针异常?

java - 列出 1 对 N 对 1 关系的内部项目的更好习惯用法?

java - 在 jsf 2.0 session 超时前 30 秒显示超时窗口

java - 使用 Hibernate HQL 从 Oracle DB 检索记录时出现性能问题

java - 配置hibernate 5.2.10时出错