2013-10-23 51 views
4

我有一個JAR文件,需要部署到多個環境中,每個環境都有自己的數據庫連接參數。無論正確還是錯誤,我都被要求將這些數據庫連接參數設置爲JAR的外部參數。我見過persistence.xml可以引用包含特定連接參數的外部hibernate.cfg.xml的示例。這裏是我的persistence.xml:如何從persistence.xml引用外部hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 

    <persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL"> 
     <properties> 
      <property name="hibernate.ejb.cfgfile" value="./hibernate.cfg.xml" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

外部hibernate.ejb.cfg位於同一文件夾中的JAR文件,但引用失敗。這裏是我得到的錯誤:

DEBUG Ejb3Configuration - Look up for persistence unit: myApp 
DEBUG Ejb3Configuration - Detect class: true; detect hbm: true 
DEBUG Ejb3Configuration - Detect class: true; detect hbm: true 
DEBUG Ejb3Configuration - Creating Factory: myApp 
ERROR MyDaoImpl - initEntityManager() exception: 
javax.persistence.PersistenceException: [PersistenceUnit: myApp] Unable to configure EntityManagerFactory 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34) 
    at com.touchnet.MyApp.server.dao.MyDaoImpl.initEntityManager(MyDaoImpl.java:486) 
    at com.touchnet.MyApp.server.dao.MyDaoImpl.getEntityManager(MyDaoImpl.java:457) 
    at com.touchnet.MyApp.server.dao.MyDaoImpl.getTransaction(MyDaoImpl.java:512) 
    at com.touchnet.MyApp.server.dao.MyDaoImpl.beginTransaction(MyDaoImpl.java:502) 
    at com.touchnet.MyApp.server.service.MyAppService.loadInputFile(MyAppService.java:346) 
    at com.touchnet.MyApp.server.commandLine.MyAppLoader.main(MyAppLoader.java:74) 
    at com.touchnet.MyApp.server.commandLine.MyAppLauncher.main(MyAppLauncher.java:44) 
Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found 
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147) 
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1433) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:753) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253) 
    ... 10 more 
ERROR MyDaoImpl - No EntityManager configured. 
DEBUG MyDaoImpl - getTransaction() returns null 
ERROR MyDaoImpl - No Transaction configured. 

如何從persistence.xml訪問外部hibernate.cfg.xml?

+0

我不確定,但是,你有沒有在你的斜槓之前點沒有嘗試。我認爲,如果你編寫'value =「/ hibernate.cfg.xml'',你的服務器會在你的lib/classes文件夾中找到你的配置文件。 – Patouche

+0

它看起來像它需要在你的類路徑上,即使它是外部的jar文件。把它放在一個目錄中,並將該目錄添加到你的類路徑中。每當你看到'getResourceAsStream'時,你可能都需要看類路徑。 – ngreen

+0

檢查這個stackoverflow答案:http://stackoverflow.com/questions/23384413/how-to-externalize-properties-of-persistence-xml-for-jboss-7-1-1 –

回答

1

錯誤這裏是

Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found 

正如在評論中指出,它看起來它WEB-INF/clases 將文件複製到該文件夾​​的文件夾中,並更改以下行到:

<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" /> 

乾杯。