如何编写Java代码让JPA / hibernate知道在哪里寻找配置文件? [英] how to write Java Code to let JPA/hibernate know where to look for Configuration files?

查看:165
本文介绍了如何编写Java代码让JPA / hibernate知道在哪里寻找配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA,hibernate和Java本身的新手。
我以某种方式使我的代码工作,以访问来自数据库的值。

I'm a newbie to JPA, hibernate and Java itself. I have somehow made my code work to access values from a DB.

当我提取我的最后jar jar,我有一个META-INF目录有persistence.xml在其中。

When I extract my final jar of jars, I have a META-INF directory which has persistence.xml in it.

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

The hibernate.properties and hibernate.cfg.xml are also inside my jar. So at runtime I'm unable to do any changes to these files since everything is inside jar.

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

I want to move persistence.xml, hibernate.cfg.xml and hibernate.properties outside jar and place them somewhere in deployment environment. I can do this but I don't know how to write Java Code to let hibernate know where to look for these files.

例如。对于log4j我可以使用PropertyConfigurator.configure(/ opt / somepath / log4j.properties)并告诉log4j其中log4j.properties文件存在。

E.g. for log4j I can use PropertyConfigurator.configure("/opt/somepath/log4j.properties") and tell log4j where log4j.properties file is present.

我会引导我到一些链接,我可以学习在运行时加载JPA和hibernate的配置。

Could anyone please help me and direct me to some links where I can learn to load configuration for JPA and hibernate at runtime ?

我想在运行时指向不同的DB,我的罐子。

I want to point to different DBs at runtime and test them without changing my jar of jars.

推荐答案

jpa / 在类路径中寻找配置文件。您可以随时添加到classpath目录/目录。

jpa/hibernate scans classpath where looking for configuration files. You can always add to classpath directory/directories.

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

Put META-INF directory to ${MY_DIR1} ( or/and ${MY_DIR2})

这篇关于如何编写Java代码让JPA / hibernate知道在哪里寻找配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆