首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性hibernate.cfg.xml

属性hibernate.cfg.xml
EN

Stack Overflow用户
提问于 2016-05-19 17:33:26
回答 1查看 362关注 0票数 0

如何将属性从hibernate.cfg.xml作为字符串加载到我的java程序?

我想用这个属性创建IDataBaseTester,或者告诉我从hibernate.cfg.xml创建IDataBaseTester的另一种方法

用于创建会话的HibernateUtil

代码语言:javascript
复制
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();
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-19 18:01:30

您不能在Hibernate 4中使用AnnotationConfiguration,只需使用Configuration

hibernate.cfg.xml获取属性

代码语言:javascript
复制
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();

代码语言:javascript
复制
StandardServiceRegistryBuilder registryBuilder = 
        new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();

或者您可以尝试使用ConfigLoader

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37330319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档