Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a not-deprecated session-factory

I am dealing with hibernate and as I opened my current project I figured out my Session-Factory is deprecated:

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

AnnotationConfiguration seems to be deprecated by now... So I checked the JavaDoc and I got told it moved to:

org.hibernate.cfg.Configuration

My code works fine so far, actually I don't want to change it... But I googled and found someone who's asking himself the same question whythe SessionFactory needs to be changed... http://rgordon.co.uk/blog/2012/02/24/hibernate-please-dont-deprecate-yourself/

The Article is from 2012 (so not that old...) and describes everything in that way:

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

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

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

I implemented that. The JavaDoc proves that wrong - again! Deprecated. It refers to:

org.hibernate.boot.registry.StandardServiceRegistryBuilder

I googled that again. The results weren't so satisfying...

I started to modify the code...

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

And an exception is thrown...

org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

at line:

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

I am pretty sure this is because I haven't specified any configuration-settings. Actually, I don't want to. I feel comfortable with the hibernate.cfg.xml.

I played a bit around with configuration.addFile(.. - wasn't that successful...

Has anyone an idea about that? Thanks

UPDATE: (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>

UPDATE (16. Feb 2014): I think it is necessary to show you my pom.xml as well. It took me a while until I figured out which combination of the Hibernate-Framework works for me...

<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>
like image 380
Qohelet Avatar asked Jul 28 '13 18:07

Qohelet


People also ask

What is difference between Session and Session factory?

SessionFactory is a factory class for Session objects. It is available for the whole application while a Session is only available for particular transaction. Session is short-lived while SessionFactory objects are long-lived. SessionFactory provides a second level cache and Session provides a first level cache.

What is Session factory in hibernate?

The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.


2 Answers

I think for 4.3 it's now:

   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);
like image 187
Owain Cole Avatar answered Oct 11 '22 11:10

Owain Cole


yes, in hibernate version 4. 3 both buildSessionfactory() and ServiceRegistryBuilder() are deprecated.

so u have change your coding like below to get session factory for hibernate version 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();
}

}

like image 2
Bharathiraja Avatar answered Oct 11 '22 10:10

Bharathiraja