Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate entity manager looking for tables in different schemas

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

like image 328
Prashant Avatar asked Nov 19 '13 11:11

Prashant


People also ask

How would you use Hibernate for two different schemas in a single database?

You can specify it by schema element while defining table for your entity. Else, you can use separate EntityManager pointing to respective schema & then use the same entity, as their structure is similar.

What ways are available to you to map an object to database entities in Hibernate?

There are two ways: (1) using annotations in Java code, and (2) using XML files using either JPA standard format or Hibernate hbm. xml files.

How to define schema in Hibernate?

You just have to add the @Table annotation to your entity class and set the name and schema attributes. @Entity @Table(name = "author", schema = "bookstore") public class Author { … } When you now use the entity, Hibernate uses the provided schema and table names to create the SQL statements.


1 Answers

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

like image 168
Anuj Panwar Avatar answered Oct 26 '22 21:10

Anuj Panwar