简体   繁体   English

Hibernate 实体管理器在不同模式中查找表

[英]Hibernate entity manager looking for tables in different schemas

I am implementing jpa persistence using hiberante-entity manager in a java web project.我正在 java web 项目中使用 hiberante-entity manager 实现 jpa 持久性。 I have set following property in in persistence.xml.我在persistence.xml 中设置了以下属性。

<property name="hibernate.hbm2ddl.auto" value="update"/>

I have a schema for each user.我为每个用户都有一个架构。 For eg i have a schema for user1 and one for user2.例如,我有一个用于 user1 的架构和一个用于 user2 的架构。 If the table 'ABC' is present in user1 schema but not in user2 schema & I deploy the application and it uses user2 db credentials, i get the message 'user1.ABC' table found so the 'ABC' table is not created in user2 schema.如果表“ABC”存在于 user1 架构中但不存在于 user2 架构中 & 我部署应用程序并且它使用 user2 db 凭据,我会收到消息“user1.ABC”表,因此“ABC”表未在 user2 中创建架构。

When i tried with following property in the persistence.xml file the table is created in the user2 schema.当我尝试在 persistence.xml 文件中使用以下属性时,该表是在 user2 模式中创建的。

 <property name="hibernate.hbm2ddl.auto" value="create"/>   

My question is why hibernate is searching in another schema ie user1 if the application is using user2 db credentials?我的问题是,如果应用程序使用 user2 db 凭据,为什么 hibernate 会在另一个模式中搜索,即 user1? and I don't want to create the schema every time the server is started so how can i avoid using the value 'create'.并且我不想在每次服务器启动时都创建架构,所以我如何避免使用值“create”。

EDIT: Below is my persistence.xml file编辑:下面是我的 persistence.xml 文件

    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="XXXXXX" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>org.axonframework.saga.repository.jpa.SagaEntry</class>
    <class>org.axonframework.saga.repository.jpa.AssociationValueEntry</class>

    <properties>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/> 
    </properties>
</persistence-unit>
</persistence>

Thanks in advance提前致谢

I am also facing the same issue, and after digging a lot, get to know that the bug is related to the Mysql Connector.我也面临同样的问题,经过大量挖掘,发现该错误与Mysql Connector有关。 After changing MySql Connector 6.0.5 to 5.1.28 It works fine for me.将 MySql Connector 6.0.5更改为5.1.28 后,它对我来说很好用。 I hope It can help you.我希望它可以帮助你。 Cheers干杯

It is not possible. 这不可能。 Their should be some configuration mistake else environment problem.. Kindly post your configuration file here for further assistance.. 他们应该是一些配置错误,否则是环境问题。请在此处发布您的配置文件以获取更多帮助。

Cheers! 干杯!

Has the same problem.有同样的问题。 After set <property name="hibernate.default_schema" value="MY_SCHEMA"/> the problem has been solved.设置<property name="hibernate.default_schema" value="MY_SCHEMA"/>后问题就解决了。

You can try this for set specific schema name : <property name="hibernate.default_schema" value="yourSchemaName"/> 您可以尝试使用此方法来设置特定的架构名称: <property name="hibernate.default_schema" value="yourSchemaName"/>

For Avoiding creation of schema you should try <property name="hibernate.hbm2ddl.auto" value="update"/> . 为了避免创建架构,您应该尝试<property name="hibernate.hbm2ddl.auto" value="update"/> It can be helpful 会有所帮助

检查您是否在 Hibernate Sessionfactory 中调用 user1 和 user2。

If you want to handle several schemas properly then use multi-tenant per schema also if you want to update/create/migrate/handle columns/tables/schemas/databases then use flyway or liquibase如果要处理several schemas正确,然后使用multi-tenant per schema还,如果你想更新/创建/迁移/手柄columns/tables/schemas/databases ,然后使用flywayliquibase

REFERENCES参考文献

Multitenancy https://vladmihalcea.com/hibernate-database-schema-multitenancy/多租户https://vladmihalcea.com/hibernate-database-schema-multitenancy/

Flyway https://flywaydb.org Flyway https://flywaydb.org

Liquibase https://www.liquibase.org Liquibase https://www.liquibase.org

The Hibernate documentation is clear about this, you need to enable multi-tenant operations as described in this answer and this example . Hibernate 文档对此很清楚,您需要启用本答案本示例中所述的多租户操作。

Basically you have to declare multiple persistence units and have each point to a different schema.基本上,您必须声明多个持久性单元并让每个单元指向不同的模式。 Each can then use different login credentials as well.然后每个人也可以使用不同的登录凭据。

Hibernate documentation link Hibernate 文档链接

To summarise:总结一下:

  1. Define your persistence unit定义你的持久化单元
  2. Define your mapping files per persistence unit定义每个持久性单元的映射文件

When using JPA add the following: 3. Specifying tenant identifier from SessionFactory 4. Implement a MultiTenantConnectionProvider使用 JPA 时添加以下内容: 3. 从 SessionFactory 指定租户标识符 4. 实现 MultiTenantConnectionProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM