简体   繁体   中英

Hibernate 5 with JPA : hibernate access without schema spécified in entity

I have a MAVEN + SPRING + JPA + HIBERNATE + MSYQL application. I want to upgrade hibernate 4.3.11 to the last version of hibernate v5.

before

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.11.Final</version> 
</dependency>

after

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.1.0.Final</version>
</dependency>

    

But when I try to access to a entity which table isn't in default schema, I have a error. Before the upgrade, it was good. Access a entity which table is in default schema works with V5 hibernate.

Default schema is "roles" but in all the entity of my application, I explicit the schema in @table JPA Annotation. Even if schema is "roles"

The Entity :

@Entity
@Table(schema="modeler",name="concept")

public class Concept {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false,  updatable = false)
private int idconcept;

@Column(name="action_date", nullable=false)
protected LocalDate actionDate;

...

public Concept() {}

...

}

When I try a read access with this entity, I have the Java error:

Exception: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

In the stack, the cause is: 
         "errorCode": 1146,
        "nextException": null,
        "sqlstate": "42S02",
        "localizedMessage": "Table 'roles.concept' doesn't exist",
        "message": "Table 'roles.concept' doesn't exist",
        "suppressed": []

in the log of hibernate, I saw the SQL order doesn't contain the schema.

Sql order with hibernate 5 

    select concept0_.idconcept as idconcep1_0_, concept0_.action_date a saction_d2_0_, ..., from concept concept0_

Sql order before with hibernate 4.3.11

    select concept0_.idconcept as idconcep1_0_, concept0_.action_date a saction_d2_0_, ..., from modeler.concept concept0_

When I try to persist the entity, I have an error in the same theme but on the hibernate_sequence table

Exception: org.hibernate.exception.SQLGrammarException: error performing isolated work

In the stack, the cause is 
        "errorCode": 1146,
         "nextException": null,
         "sqlstate": "42S02",
        "localizedMessage": "Table 'roles.hibernate_sequence' doesn't exist",
        "message": "Table 'roles.hibernate_sequence' doesn't exist",
        "suppressed": []

in the log of hibernate, the SQL order doesn't contain the schema.

     select next_val as id_val from hibernate_sequence for update

--> but, it's seems that it's the same schema problem as in read access.

So I tried to find a solution before my question. I found on hibernate site that version v5.0.8 was stable and tried it. I had the same problem with it.

So I read the evolutions of the V5.0.8. The only change which have focused my attention was : Naming strategies I modified my xml spring configuration with some solution find on the web . But, it doesn't work.

Extract of my xml spring configuration

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:config/persistence.xml" />
    <property name="persistenceUnitName" value="demoRestPersistence" />
    <property name="dataSource" ref="restDemoDS" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        </bean>
    </property>
    
     <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.show_sql" value="true" />
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <entry key="hibernate.implicit_naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl" />
        </map>
    </property>
    
</bean>

<bean id="restDemoDS"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    scope="singleton">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/roles" />
    <property name="username" value="***" />
    <property name="password" value="******" />
</bean>

So I found the problem.

Since version 5 of hibernate (don't know why and what), if you access to a database MySQL with several schema, this annotation is no correct anymore:

@Table(schema="modeler",name="concept")

You have to complete the annotation with the parameter catalog

@Table(catalog="modeler",schema="modeler",name="concept").

With this complement, the application runs. More information on the catalog property can be found in the hibernate user guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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