Разбираюсь, что такое Hibernate и как он работает, возникла проблема:
При создании сессии методом:
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static ServiceRegistry serviceRegistry = null;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
Все отлично работает, однако метод buildSessionFactory() отмечен как deprecated, вместо него рекомендуется использовать buildSessionFactory(serviceRegistry), но при попытке создать сессию следующим образом:
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static ServiceRegistry serviceRegistry = null;
static {
try {
//creates the session factory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).getBootstrapServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
возникает ошибка:
дек 26, 2013 1:28:48 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
дек 26, 2013 1:28:48 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.Final}
дек 26, 2013 1:28:48 PM org.hibernate.cfg.Environment
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
дек 26, 2013 1:28:48 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
дек 26, 2013 1:28:48 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
дек 26, 2013 1:28:48 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
дек 26, 2013 1:28:48 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace
hibernate.sourceforge.net/. Use namespace
www.hibernate.org/dtd instead. Refer to Hibernate 3.6 Migration Guide!
дек 26, 2013 1:28:48 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
java.lang.NullPointerException
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at Hibernate.Util.HibernateUtil.(HibernateUtil.java:18)
at Hibernate.DAO.Impl.StudentDAOImpl.addStudent(StudentDAOImpl.java:17)
at Main.main(Main.java:22)
Что я делаю не так?
Заранее благодарен.