6

I am implementing jpa persistence using hiberante-entity manager in a java web project. I have set following property in in persistence.xml.

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

I have a schema for each user. For e.g i have a schema for user1 and one for 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.

When i tried with following property in the persistence.xml file the table is created in the user2 schema.

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

My question is why hibernate is searching in another schema i.e user1 if the application is using user2 db credentials? and I don't want to create the schema every time the server is started so how can i avoid using the value 'create'.

EDIT: Below is my persistence.xml file

    <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

Prashant
  • 81
  • 7

5 Answers5

3

I am also facing the same issue, and after digging a lot, get to know that the bug is related to the Mysql Connector. After changing MySql Connector 6.0.5 to 5.1.28 It works fine for me. I hope It can help you. Cheers

Anuj Panwar
  • 113
  • 7
0

Has the same problem. After set <property name="hibernate.default_schema" value="MY_SCHEMA"/> the problem has been solved.

PETRo
  • 173
  • 1
  • 7
0

Check if you are calling user1 and user2 in your Hibernate Sessionfactory.

morrisng
  • 127
  • 7
  • In my case there is only one user (no multi-tenancy) and behavior is the same as described by the author. – Scadge Oct 28 '19 at 07:37
  • @Scadge having multiple schemas implicitly means multi-tenancy. Each schema is considered a tenant. – jwenting Oct 29 '19 at 04:25
0

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

REFERENCES

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

Flyway https://flywaydb.org

Liquibase https://www.liquibase.org

Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
  • In my case there's no multi-tenancy - only a single user and connection string to specific database, but instead of creating a table in this database, hbm2ddl updates the one in another schema which appears to have the same name. – Scadge Oct 28 '19 at 07:38
  • @Scadge and please don't hijack other peoples' questions to ask your own. – jwenting Oct 29 '19 at 04:27
0

The Hibernate documentation is clear about this, you need to enable multi-tenant operations as described in this answer and this example.

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

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

jwenting
  • 5,505
  • 2
  • 25
  • 30