2012-08-07 50 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