休眠如何使架构名称可配置为实体类 [英] Hibernate how to make schema name configurable for entity class

查看:66
本文介绍了休眠如何使架构名称可配置为实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使Entity类中的架构名称可配置.下面给出了我为springboot应用程序尝试的代码.

I have been tying to make the schema name in Entity class configurable. The code which I tried for my springboot applicqtion is given below.

    @Entity
    @Table(name="Employee", schema=Employee.schemaName)
             public class Employee implements Serializable {
        @Autowired
        private static Environment env;
        public static String schema =env.getProperty("databaseSchema");
        public static final String schemaName = schema; 
        ..........
        }

在这里,我试图使用Environment对象从配置文件中获取架构名称,并将其值设置为@Table批注中的架构.但是我在@Table批注中看到架构名称错误,批注属性Table.schema的值必须是一个常量表达式".而且,如果我从变量schema和schemaName中删除了静态的final,那么@Table中也会出现一个错误,无法对非静态字段Employee.schemaName进行静态引用".有什么方法可以使架构名称可配置?预先感谢.

Here I was trying to get the schema name from my configuration file using Environment object and set the value to schema inside @Table annotation. But I'm getting an error at the schema name in @Table annotation that "The value for annotation attribute Table.schema must be a constant expression". And if I remove the static final from the variables schema and schemaName, then also am getting an error inside @Table that "cannot make a static reference to the non-static field Employee.schemaName". Is there any way by which I can make the schema name configurable? Thanks in advance.

推荐答案

也许是Hibernate多租户

Maybe Hibernate multi-tenancy

请参见文档此处

多租户允许在单个托管服务器上为多个租户提供单个应用程序实例.通常,这可以通过分离数据库,分离架构或共享架构来完成.

Multi-tenancy allows a single application instance to be served for multiple tenants on a single hosting server. This is usually performed by either Separating databases, Separating schemas or Sharing schemas.

这篇关于休眠如何使架构名称可配置为实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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