Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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中设置@Table_Spring_Hibernate_Jpa - Fatal编程技术网

在运行时Spring中设置@Table

在运行时Spring中设置@Table,spring,hibernate,jpa,Spring,Hibernate,Jpa,我正在编写一个spring服务,使用JPA连接到我的PostgreSQL服务器。我面临的问题是如何在运行时将@Table设置为一个值。我有两张桌子,一张用于QA,另一张用于prod。因此,当我执行jar时,我将概要文件设置为QA或prod,但我无法获得如何将@Table设置为给定概要文件的对应表 java -jar -Dspring.config.location=.\vmconfig -Dspring.profiles.active=qa postgre-1.0.1.jar Properti

我正在编写一个spring服务,使用JPA连接到我的PostgreSQL服务器。我面临的问题是如何在运行时将@Table设置为一个值。我有两张桌子,一张用于QA,另一张用于prod。因此,当我执行jar时,我将概要文件设置为QA或prod,但我无法获得如何将@Table设置为给定概要文件的对应表

java -jar -Dspring.config.location=.\vmconfig -Dspring.profiles.active=qa postgre-1.0.1.jar

Properties File

server.port= 6869   
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://xxx-xxx/DB
spring.datasource.username=postgres
spring.datasource.password=xxxx
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
@Tablename=->应在运行时根据执行jar的配置文件设置名称。

您有三个选项:

建议使用orm.xml 您可以定义orm.xml。使用它,可以覆盖实体的表名。 通常,您会将orm.xml放在resources/META-INF文件夹中。但这将适用于您的所有配置文件,因为SpringBoot将自动加载它

对于您的情况,您只希望它用于指定的配置文件。为此,需要创建LocalContainerEntityManagerFactoryBean。而不仅仅是设置属性

然后,可以在LocalContainerEntityManagerFactoryBean上设置orm.xml的位置

例如:

@Bean
@Profile("QA")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
    bean.setDataSource(dataSource);
    bean.setJpaVendorAdapter(jpaVendorAdapter);
    bean.setPackagesToScan("com.example.demo");
    bean.setMappingResources("orm.xml");
    return bean;
}
此配置应仅应用于所需的配置文件

下面是orm.xml的一个简单示例

<entity-mappings version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">

<entity class="com.example.demo.EntityName">
    <table name="NEW_TABLE_NAME"></table>
</entity>

**另外:只需检查如何构造数据库命名要求的标识符

这就是JPA规范定义orm.xml的原因。。。
@Bean
public SpringPhysicalNamingStrategy springPhysicalNamingStrategy() {
    return new SpringPhysicalNamingStrategy() {
        @Override
        public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
            // Just change find and replace your table name
            return super.toPhysicalTableName(new Identifier( name.getText(), false), jdbcEnvironment);
        }
    };
}