[英]Hibernate 4 Annotation Configuration
I'm trying to use Hibernate 4 with annotations only, and a hibernate.cfg.xml
file. 我正在尝试仅使用带有注释的Hibernate 4和一个
hibernate.cfg.xml
文件。 I've made my own annotation and am using reflection to add this to the configuration. 我已经制作了自己的注释,并使用反射将其添加到配置中。 I'm able to use Hibernate 4 in this manner fine, but my configuration is being built using a deprecated method.
我能够以这种方式使用Hibernate 4,但我的配置是使用不推荐的方法构建的。
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
return configuration.configure().buildSessionFactory();
(Deprecated code: buildSessionFactory();
). (不推荐使用的代码:
buildSessionFactory();
)。
Even the hibernate 4 documentation shows to build the configuration in that manner. 即使是hibernate 4文档也显示以这种方式构建配置。
If I try to use the new method ( buildSessionFactory(ServiceRegistry)
, I don't get the same result, and it seems like a lot of unnecessary code to do exactly what the deprecated method does anyway. However, I don't want to keep using this style, because I dislike using deprecated code anyway. 如果我尝试使用新方法(
buildSessionFactory(ServiceRegistry)
,我得不到相同的结果,并且看起来很多不必要的代码完全按照不推荐使用的方法做了。但是,我不想继续使用这种风格,因为我不喜欢使用弃用的代码。
My question is: How do I correctly configure Hibernate 4 from just a configuration file in the manner described above? 我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4? I seem to just cause errors & face unnecessary difficulties.
我似乎只是造成错误并面临不必要的困难。
Modified code will look like below:- 修改后的代码如下所示: -
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings
(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
You may check following links for information: HHH-6183 and HHH-2578 . 您可以查看以下链接以获取信息: HHH-6183和HHH-2578 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.