2012-08-07 53 views
0

我有我的hibernate.cfg.xml获取从类值Hibernate配置

有场:

<property name="hibernate.connection.username">username</property> // getUser()??? 

我有类Detail.java,其中有吸气:

public String getUser() { 
    return user; 
} 

如何从我的hibernate配置XML文件访问该方法?

回答

1

您无法访问hibernate.cfg.xml中的类属性。

有一种方法可以通过以编程方式配置会话工厂来实现此目的。检查hibernate docs关于如何做到这一点。

1
You can access method property from configuration file. 
But you can access configuration property value from java method 

尝试用下面的代码:

System.out.println(sessionFactory.getConfiguration().getProperty("hibernate.connection.username")) 
+1

getConfiguration()似乎不是Hibernate4中的SessionFactory中的一个方法,是否有任何其他方式来执行此操作或者它可能会移动? – bcmoney 2013-04-10 13:51:55

0

这Hibernate4工作对我来说:

File config = new File("src/hibernate.cfg.xml"); 
Configuration configuration = new Configuration().configure(getConfigurationFile()); 
String url = configuration.getProperty("hibernate.connection.url"); 

不知道为什么,但看起来像getConfiguration()方法不再可用in org.hibernate.SessionFactory