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
Spring Hibernate-设置静态最终字符串以指示Hibernate实体的模式_Spring_Hibernate - Fatal编程技术网

Spring Hibernate-设置静态最终字符串以指示Hibernate实体的模式

Spring Hibernate-设置静态最终字符串以指示Hibernate实体的模式,spring,hibernate,Spring,Hibernate,我将域对象映射为Hibernate实体,存储在Oracle数据库的两个不同模式中。不幸的是,这个模式中的一个在Test和Prod环境数据库之间没有相同的名称 为了避免每次在这样或那样的环境中部署时都手动更改代码中的模式,我的想法是使用Spring属性占位符根据环境获取属性文件中的值。然后使用MethodInvokingFactoryBean在有用的静态实用程序类中设置这些属性 根据使用Spring的PropertyPlaceHolderConfigure在属性文件中读取的属性,Spring的co

我将域对象映射为Hibernate实体,存储在Oracle数据库的两个不同模式中。不幸的是,这个模式中的一个在Test和Prod环境数据库之间没有相同的名称

为了避免每次在这样或那样的环境中部署时都手动更改代码中的模式,我的想法是使用Spring属性占位符根据环境获取属性文件中的值。然后使用MethodInvokingFactoryBean在有用的静态实用程序类中设置这些属性

根据使用Spring的PropertyPlaceHolderConfigure在属性文件中读取的属性,Spring的conf设置静态变量的值:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="com.company.app.Schemas" />
    <property name="targetMethod" value="setSchemaNames" />
    <property name="arguments">
        <props>
            <prop key="schema1">${schema1_name}</prop>
            <prop key="schema2">${schema2_name}</prop>
        </props>
    </property>
</bean>
然而,要设置@Table Hibernate注释的schema属性,我不能使用“static”变量,我必须使用“final static”变量(常量)。因此,上面的最后一段代码无法编译


我没有成功地使用Spring的MethodInvokingFactoryBean设置“final static”变量。我该怎么办?

理论上,您可以创建一个类来访问
public static final
字段和us reflection,使它们可以访问并设置值

Field field = ReflectionUtils.findField(Schemas.class, "SCHEMA1");
field.setAccessible(true);
ReflectionUtils.setField(field, target, "MySchema");
然而,即使有了这一点,它也永远不会起作用。
类中的常量将在编译时与实际值内联。如果要反编译
类,它将如下所示

@Entity
@Table(schema = null, name = "item")
public class Item {
  ...
}
这就是常量在java(或者更好的是java编译器)中的工作方式

我将域对象映射为Hibernate实体,存储在Oracle数据库的两个不同模式中。不幸的是,这个模式中的一个在Test和Prod环境数据库之间没有相同的名称

这可能对您的解决方案有所帮助。基本上,在您的实体中,在test en prod中具有常量模式的实体定义了该模式。对于其他实体(test en prod中的不同模式),不定义模式,只需在配置中设置
hibernate.default_schema
属性

Field field = ReflectionUtils.findField(Schemas.class, "SCHEMA1");
field.setAccessible(true);
ReflectionUtils.setField(field, target, "MySchema");
@Entity
@Table(schema = null, name = "item")
public class Item {
  ...
}