Hibernate - AnnotationConfiguration deprecated

2019-01-23 01:16发布

问题:

I am using Hibernate in version 3.6.0 and the AnnotationConfiguration is marked as deprecated.

Here is the the line in my HibernateUtil.java class:

sessionFactory = new AnnotationConfiguration().configure("/hib.cfg.xml").buildSessionFactory();

What's the replacement for AnnotationConfiguration?

回答1:

"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



回答2:

Just do this

import org.hibernate.cfg.Configuration;

and then change your code for this

sessionFactory = new Configuration().configure("/hib.cfg.xml").buildSessionFactory(); 


回答3:

I use this code:

Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();

sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);


回答4:

yes it is working for me-

Configuration cfg=new Configuration();
    cfg.configure();

    ServiceRegistry serviceregistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();


    Session session=cfg.configure().buildSessionFactory(serviceregistry).openSession();