寻找一个不被弃用的会话工厂

时间:2013-07-28 18:17:14

标签: java hibernate hibernate-annotations sessionfactory sql-server-2012-express

我正在处理hibernate,当我打开当前项目时,我发现我的Session-Factory已被弃用:

AnnotationConfiguration af = new AnnotationConfiguration();
SessionFactory factory = af.configure().buildSessionFactory();
Session session = factory.openSession();

AnnotationConfiguration现在似乎已经被弃用了......所以我查了一下JavaDoc,我告诉它转移到:

  

org.hibernate.cfg.Configuration

我的代码到目前为止工作得很好,实际上我不想改变它......但是我用Google搜索并发现有人在问自己同样的问题为什么SessionFactory需要更改... http://rgordon.co.uk/blog/2012/02/24/hibernate-please-dont-deprecate-yourself/

该文章来自2012年(所以不是那么古老......)并以这种方式描述所有内容:

ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(properties);
ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();

Configuration configuration = new Configuration().addClass(FeedTradePersistable.class);

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

我实现了那个。 JavaDoc再次证明了错误!已过时。 它指的是:

  

org.hibernate.boot.registry.StandardServiceRegistryBuilder

我又搜索了一遍。结果并不那么令人满意......

我开始修改代码......

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();

抛出异常......

  

org.hibernate.HibernateException:连接时不能为null   'hibernate.dialect'没有设置

在线:

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

我很确定这是因为我没有指定任何配置设置。实际上,我不想。我对hibernate.cfg.xml感到满意。

我使用configuration.addFile玩了一下(.. - 不是那么成功......

有人对此有所了解吗? 感谢

更新:(hibernate.cfg.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost\SQLEXPRESS</property>
    <property name="hibernate.connection.username">qohelet</property>
    <property name="hibernate.connection.password">password</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="bonecp.setIdleMaxAgeInMinutes">240</property>
    <property name="bonecp.setIdleConnectionTestPeriodInMinutes">5</property>
    <property name="bonecp.partitionCount">3</property>
    <property name="bonecp.acquireIncrement">10</property>
    <property name="bonecp.maxConnectionsPerPartition">60</property>
    <property name="bonecp.minConnectionsPerPartition">20</property>
    <property name="bonecp.statementsCacheSize">50</property>
    <property name="bonecp.releaseHelperThreads">3</property>
        <mapping class="order.Line" />
        <mapping class="order.Order" />
        <mapping class="order.Group" />
  </session-factory>
</hibernate-configuration>

更新(2014年2月16日): 我认为有必要向您展示我的pom.xml。我花了一段时间才弄清楚Hibernate-Framework的哪个组合对我有用......

<dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.2.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.10.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>hibernate-commons-annotations</artifactId>
            <groupId>org.hibernate.common</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <groupId>org.hibernate.javax.persistence</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-commons-annotations</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.1.8.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>hibernate</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-annotations</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-commons-annotations</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
        <exclusion>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <groupId>org.hibernate.javax.persistence</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.2.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>1.1.1.Final</version>
    <scope>provided</scope>
</dependency>

3 个答案:

答案 0 :(得分:9)

我认为现在是4.3:

   Configuration configuration=new Configuration()
            .configure(); // configures settings from hibernate.cfg.xml

    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();

    // If you miss the below line then it will complaing about a missing dialect setting
    serviceRegistryBuilder.applySettings(configuration.getProperties());

    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

答案 1 :(得分:1)

是的,在hibernate版本4中.3不推荐使用buildSessionfactory()和ServiceRegistryBuilder()。

所以你已经改变了你的编码,如下所示,以获得hibernate版本4.3的会话工厂

public class HibernateSessionFactory {

private static final SessionFactory sessionFactory = buildSessionFactory1();

private static SessionFactory buildSessionFactory1() {
    Configuration configuration = new Configuration().configure(); // configuration
                                                                    // settings
                                                                    // from
                                                                    // hibernate.cfg.xml

    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();


    serviceRegistryBuilder.applySettings(configuration.getProperties());

    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

    return configuration.buildSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
}

}

答案 2 :(得分:1)

由于我有时间对我的软件进行现代化改造,所以我决定付出一些努力并进行一些研究:

http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry 提供现代HibernateUtil

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration().configure();
        ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
        registry.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = registry.buildServiceRegistry();

        sessionFactory = configuration.buildSessionFactory(serviceRegistry);            
    }

    return sessionFactory;
}

}

即使这个版本似乎也有效:

import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        Configuration cfg = new Configuration();
        sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (Throwable ex) {
        Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}

但我的问题是我不想将新版本与旧版本集成。更新后我遇到了

  

java.lang.NoSuchMethodError:   org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg /休眠/注解/普通/反射/ ClassLoaderDelegate;)V

常。 烦人。通常Mkyong提供了很好的解决方案,但在我的情况下,他写了Stackoverflow的相反解决方案......

所以我搜索了一些存储库,发现了一个非常简单的解决方案(比较:http://hibernate.org/orm/downloads/):

<!-- HIBERNATE -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.0.0.Final</version>
</dependency>

其他一些小问题阻止了我: 在hibernate.cfg.xml我必须将行从update更改为auto

<property name="hbm2ddl.auto">auto</property> 

将它与我的旧pom.xml进行比较...... - 然后我与Hibernate进行了第一次“相遇”,并添加了一切看似有用的内容,直到它起作用。在它这样做之后我停止了触摸它...差不多两年......永远不要改变一个“获胜”的团队,对吗?

<dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.12.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-commons-annotations</artifactId>
                    <groupId>org.hibernate.common</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-jpa-2.1-api</artifactId>
                    <groupId>org.hibernate.javax.persistence</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-commons-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.8.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-commons-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-jpa-2.1-api</artifactId>
                    <groupId>org.hibernate.javax.persistence</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>1.1.1.Final</version>
            <scope>provided</scope>
        </dependency>