繁体   English   中英

属性hibernate.cfg.xml

[英]properties hibernate.cfg.xml

我如何将hibernate.cfg.xml属性作为String加载到我的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM