简体   繁体   English

Hibernate 4注释配置

[英]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-6183HHH-2578

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于Hibernate SessionFactory注释的配置 - Hibernate SessionFactory annotation based configuration Springs3 Hibernate3批注配置 - Springs3 Hibernate3 annotation configuration 在Hibernate 4.2中从XML迁移到注释配置 - Migration from XML to Annotation configuration in Hibernate 4.2 无法在基于Spring Hibernate注释的配置中获取JDBC连接 - Unable to acquire JDBC Connection in spring hibernate annotation based configuration 如何在新版本的Hibernate中初始化注释配置实例? - How to initialize annotation configuration instance in the new version of Hibernate? Hibernate 和 Spring3 事务管理注释-配置问题:休眠异常:没有 Hibernate Session 绑定到线程 - Hibernate and Spring3 Transaction Management Annotation-Configuration problems: Hibernate-Exception: No Hibernate Session bound to thread 休眠注释 - Hibernate annotation java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass 同时使用 Hibernate Annotation 和 Maven - java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass while using Hibernate Annotation and Maven 休眠配置 - Hibernate configuration spring 3.1.0 Release Hibernate 4.1.2 Final - 通过注释进行数据库配置如何设置属性 - spring 3.1.0 Release Hibernate 4.1.2 Final - Database configuration via annotation how to set properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM