Is the buildSessionFactory() Configuration method deprecated in Hibernate?
Yes it is deprecated. Replace your SessionFactory with the following:
In Hibernate 4.0, 4.1, 4.2
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()). buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
UPDATE:
In Hibernate 4.3 ServiceRegistryBuilder is deprecated. Use the following instead.
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
buildSessionFactory() method that return Hibernate SessionFactory object is deprecated, how to solve?
In Hibernate 4, buildSessionFactory( ) is deprecated. This example contains code which you can use in spite of buildSessionFactory ().
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
You can get more details at Hibernate 4 Simple Example
Hibernate SessionFactory method deprecated
Hibernate documentation gives an example:
SessionFactory sf = new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
Looking for a not-deprecated session-factory
As I got some time to modernize my software I decided to put some effort into it and did some research:
http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry
provides a modern 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;
}
}
Even though this version seems to work as well:
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;
}
}
But my problem was I didn't want to integrate a new version with the old libs. After updating I ran into a
java.lang.NoSuchMethodError:
org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V
often.
Annoying. Usually Mkyong provides good solutions, but in my case he wrote the opposite solution of Stackoverflow...
So I searched some repositories and found a shockingly easy solution (compare: http://hibernate.org/orm/downloads/):
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.0.Final</version>
</dependency>
Some other minor issues which stopped me:
In the hibernate.cfg.xml
I had to change the line from update
to auto
:
<property name="hbm2ddl.auto">auto</property>
Compare it to my old pom.xml... - back then I had my first "encounter" with Hibernate and added everything which seemed useful until it worked. After it did so I stopped touching it for... Almost two years... Never change a "winning" team, right?
<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>
buildSessionFacotry is deprecated?
buildSessionFactory()
has been deprecated in Hibernate 4
Use buildSessionFactory(ServiceRegistry serviceRegistry)
instead
so do below:-
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory= configuration.buildSessionFactory(serviceRegistry);
Hibernate - AnnotationConfiguration deprecated
"All functionality has been moved to Configuration":
http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/AnnotationConfiguration.html
And here is Configuration:
http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/Configuration.html
Related Topics
Parsing Xml with Regex in Java
Convert from List<Completablefuture> to Completablefuture<List>
What Objects How to Inject Using the @Context Annotation
Java Garbage Collector - When Does It Collect
Adding Image to Jbutton with Foreground Label
What Are the Pros and Cons of the Assorted Java Web Frameworks
Convert JSON String to Pretty Print JSON Output Using Jackson
Java Converting Image to Bufferedimage
Java.Util.Date Format Ssssss: If Not Microseconds What Are the Last 3 Digits
Java 8 Date and Time: Parse Iso 8601 String Without Colon in Offset
Converting a Java Keystore into Pem Format
Does Okhttp Support Accepting Self-Signed Ssl Certs
Java Conditional Compilation: How to Prevent Code Chunks from Being Compiled
Incompatible Magic Value 1008813135