Hibernate 4注释配置 [英] Hibernate 4 Annotation Configuration

查看:385
本文介绍了Hibernate 4注释配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅将Hibernate 4与注释一起使用,并使用 hibernate.cfg.xml 文件。我做了自己的注释,并使用反射将其添加到配置中。我能够以这种方式使用Hibernate 4,但是我的配置是使用不赞成使用的方法构建的。

  final配置配置=新的配置(); 
final Reflections reflections = new Reflections(Item.class.getPackage()。getName());
final Set< Class <?>> classes = reflections.getTypesAnnotatedWith(Entity.class); (最终Class<> clazz:classes){
configuration.addAnnotatedClass(clazz);

返回configuration.configure()。buildSessionFactory();

(不推荐使用的代码: buildSessionFactory(); )。/ b>

即使是hibernate 4的文档也会以这种方式构建配置。



如果我尝试使用新方法( buildSessionFactory(ServiceRegistry)),我没有得到相同的结果,而且好像有很多不必要的代码来完成弃用的方法。但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码。



我的问题是:我正确地从配置文件中以上述方式正确配置Hibernate 4?我似乎只是导致错误并且面临不必要的困难。

解决方案

修改后的代码如下所示: -

$ p $ final配置配置=新配置();
final Reflections reflections = new Reflections(Item.class.getPackage()。getName()) ;
final Set< Class <?>> classes = reflections.getTypesAnnotatedWith(Entity.class); (最终Class<> clazz:classes){
configuration.addAnnotatedClass(clazz);
}
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()。applySettings
(configuration.getProperties())。buildServiceRegistry();

return configuration.buildSessionFactory(serviceRegistry);

您可以查看以下链接获取信息: HHH-6183 HHH-2578


I'm trying to use Hibernate 4 with annotations only, and a hibernate.cfg.xml file. 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.

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(); ).

Even the hibernate 4 documentation shows to build the configuration in that manner.

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.

My question is: How do I correctly configure Hibernate 4 from just a configuration file in the manner described above? 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.

这篇关于Hibernate 4注释配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆