属性 hibernate.cfg.xml
properties hibernate.cfg.xml
如何将 hibernate.cfg.xml
中的属性作为字符串加载到我的 java 程序中?
我想使用此属性创建 IDataBaseTester
或告诉我另一种从 hibernate.cfg.xml
创建 IDataBaseTester
的方法
HibernateUtil
创建会话
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
您不能在 Hibernate 4 中使用 AnnotationConfiguration
。只需使用 Configuration
。
从 hibernate.cfg.xml
获取属性
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();
或
StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();
或者你可以尝试使用
ConfigLoader
如何将 hibernate.cfg.xml
中的属性作为字符串加载到我的 java 程序中?
我想使用此属性创建 IDataBaseTester
或告诉我另一种从 hibernate.cfg.xml
IDataBaseTester
的方法
HibernateUtil
创建会话
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
您不能在 Hibernate 4 中使用 AnnotationConfiguration
。只需使用 Configuration
。
从 hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();
或
StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();
或者你可以尝试使用 ConfigLoader