Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写Java代码让JPA/hibernate知道在哪里查找配置文件?_Java_Hibernate_Jpa_Configuration - Fatal编程技术网

如何编写Java代码让JPA/hibernate知道在哪里查找配置文件?

如何编写Java代码让JPA/hibernate知道在哪里查找配置文件?,java,hibernate,jpa,configuration,Java,Hibernate,Jpa,Configuration,我是JPA、hibernate和Java本身的新手。 我以某种方式使我的代码能够从数据库访问值 当我提取最后一个jar时,我有一个META-INF目录,其中包含persistence.xml hibernate.properties和hibernate.cfg.xml也在我的jar中。 所以在运行时,我无法对这些文件进行任何更改,因为所有内容都在jar中 我想将persistence.xml、hibernate.cfg.xml和hibernate.properties移到jar之外,并将它们放在

我是JPA、hibernate和Java本身的新手。 我以某种方式使我的代码能够从数据库访问值

当我提取最后一个jar时,我有一个META-INF目录,其中包含persistence.xml

hibernate.properties和hibernate.cfg.xml也在我的jar中。 所以在运行时,我无法对这些文件进行任何更改,因为所有内容都在jar中

我想将persistence.xml、hibernate.cfg.xml和hibernate.properties移到jar之外,并将它们放在部署环境中的某个地方。我可以这样做,但我不知道如何编写Java代码让hibernate知道在哪里查找这些文件

例如,对于log4j,我可以使用PropertyConfigurator.configure(“/opt/somepath/log4j.properties”),并告诉log4j log4j.properties文件存在的位置

有谁能帮我一下,并告诉我一些链接,在那里我可以学习在运行时加载JPA和hibernate的配置

我想在运行时指向不同的DBs并测试它们,而不改变jar的jar。

/p>/scans classpath,在其中查找配置文件。您始终可以添加到类路径目录

    MY_DIR1 = path/to/my/dir1
    My_DIR2 = /path/to/my/dir2
java -cp ${CLASSPATH}:${MY_DIR1}:${MY_DIR2} ...

META-INF目录
放到${MY_DIR1}(或/和${MY_DIR2})

在ajozwik的帮助下,我解决了我的问题如下->

  • 在部署系统的某个位置创建了/path/to/mydir目录
  • 已将META-INF、hibernate.properties和hibernate.cfg.xml移动到mydir

    目录结构->

    mydir

  • 在启动可执行文件时将mydir添加到类路径

  • 而不是使用

    java -jar Myjar.jar
    
    我使用

    java -cp Myjar.jar:/path/to/mydir MyMainClass
    

  • 谢谢你的回答。我像这样尝试了java-cp${CLASSPAH}:./path/to/my/dir-jar Myjar.jar,但是web上的一些链接说cp不能与-jar一起使用。任何解决办法。我已经使用mvn clean install和mvn assembly:single构建了我的项目Myjar.jar,它是一个jar的jar。如果我使用-cp选项,我将如何从命令行启动我的jar?如果您使用此选项,jar文件是所有用户类的源,其他用户类路径设置将被忽略。因此,请解压缩您的jar文件。
    java -cp Myjar.jar:/path/to/mydir MyMainClass