属性hibernate.cfg.xml

时间:2016-05-19 17:33:26

标签: java hibernate dbunit

我如何将hibernate.cfg.xml的属性作为String加载到我的java程序中?

我想使用此属性创建IDataBaseTester或告诉我IDataBaseTester

创建hibernate.cfg.xml的另一种方法

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();
    }

}

1 个答案:

答案 0 :(得分:0)

您无法将AnnotationConfiguration与Hibernate一起使用4.只需使用Configuration

hibernate.cfg.xml

获取属性
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();

StandardServiceRegistryBuilder registryBuilder = 
        new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();

或者您可以尝试使用 ConfigLoader