在运行时Spring中设置@Table [英] Setting @Table in run time Spring

查看:172
本文介绍了在运行时Spring中设置@Table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a spring service to connect to my PostgreSQL SQL server using JPA . The problem that I am facing is how to set @Table to a value at run time . I am having two tables , one for QA and other for prod . So when I execute the jar , I am setting the profile to QA or prod , but I am not able to get how to set @Table to corresponding table for the given profile .

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

@Table(name =<>)->应该在运行时根据执行jar的配置文件来设置名称.

@Table(name=<>) --> The name should be set at runtime based on the profile in which jar is executed.

推荐答案

您有三个选择:

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

You can define an orm.xml. With it, you can override the entity's table name. Normally you would have dropped the orm.xml in your resources/META-INF folder. But this will then apply for all your profiles, as Spring Boot will load it automatically.

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

For your case, you want it only for a specified profile. For this, you need to create the LocalContainerEntityManagerFactoryBean. (Instead of just setting the properties) (Example here)

LocalContainerEntityManagerFactoryBean上,您可以设置orm.xml的位置.

On LocalContainerEntityManagerFactoryBean you can then set location for 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;
}

此配置仅应随后应用于所需的配置文件.

This configuration should only then be applied to the required profile.

这是orm.xml的简单示例

Here is a simple example of an 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>

*感谢@BillFrost指出这一点.

*Thanks @BillFrost for pointing this out.

您可以为每个环境配置提供不同的@EntityScan.因此,仅扫描QAConfiguration中的QA实体.这要求您使用@Table中定义的不同名称创建重复的实体.

You can supply different @EntityScan per environment configuration. Thus scan for only QA entities in the QAConfiguration. This requires you to create duplicate Entities with different names defined in the @Table.

我真的不喜欢这样,因为它会使您不得不维护实体的QA集和生产集. 这只是一个正在发生的生产问题.

I really do not like this, as it causes you to have to maintain a QA set of entities and a production set. This is just a production issue waiting to happen.

最后,您可以扩展SpringPhysicalNamingStrategy,然后可以修改该特定的表名.然后,该bean应该仅在QA配置文件中处于活动状态.

Lastly, you can extend the SpringPhysicalNamingStrategy, you can then modify that specific table name. This bean then should only be active in the QA profile.

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

** ALSO:只需检查如何根据数据库的命名要求构造Identifier即可.

**ALSO: Just check how to construct an Identifier to your DB's naming requiements.

这篇关于在运行时Spring中设置@Table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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