如何使用 Guice 将属性注入/解析到 XML 文件中以配置 persistence.xml 文件?

发布于 2024-12-07 04:46:03 字数 1648 浏览 0 评论 0原文

我正在使用 guice-persist,我想知道是否有任何可以将 Java 属性文件中的属性注入或解析器属性到 persistence.xml 文件中,就像我对 Spring 所做的那样。例如:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
    version="2.0">

    <persistence-unit name="WoloxShivaJPAUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.guidomb.MyClass</class>

        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="hibernate.connection.driver_class" value="${hibernate.connection.driver_class}">
            <property name="hibernate.connection.url" value="${hibernate.connection.url}">
            <property name="hibernate.connection.username" value="${hibernate.connection.username}">
            <property name="hibernate.connection.password" value="${hibernate.connection.password}">
            <property name="hibernate.dialect" value="${hibernate.dialect}">
            <property name="hibernate.id.new_generator_mappings" value="true">
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        </properties>

    </persistence-unit>
</persistence>

如果不是,我如何使用 guice-persist 库配置 EntityManager 以便从属性文件注入此属性?

谢谢!

I'm using guice-persist and I would like to know if there is any to inject or resolver properties from a Java property file into the persistence.xml file like I would do with Spring. For example:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
    version="2.0">

    <persistence-unit name="WoloxShivaJPAUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.guidomb.MyClass</class>

        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="hibernate.connection.driver_class" value="${hibernate.connection.driver_class}">
            <property name="hibernate.connection.url" value="${hibernate.connection.url}">
            <property name="hibernate.connection.username" value="${hibernate.connection.username}">
            <property name="hibernate.connection.password" value="${hibernate.connection.password}">
            <property name="hibernate.dialect" value="${hibernate.dialect}">
            <property name="hibernate.id.new_generator_mappings" value="true">
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        </properties>

    </persistence-unit>
</persistence>

If not how can I configure the EntityManager using the guice-persist libray so as to inject this properties from a property file?.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2024-12-14 04:46:03

看起来您可以在创建 JpaPersistModule 时提供其他属性:

JpaPersistModule jpa = new JpaPersistModule("myFirstJpaUnit");
jpa.properties(...);
Injector injector = Guice.createInjector(..., jpa);

It looks like you can supply additional properties when creating JpaPersistModule:

JpaPersistModule jpa = new JpaPersistModule("myFirstJpaUnit");
jpa.properties(...);
Injector injector = Guice.createInjector(..., jpa);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文