2016-03-08 48 views
4

(我的英語很抱歉,這不是我的自然語言)休眠5 JPA:無冬眠模式在實體指定的訪問

所以,我有一個Maven + SPRING + JPA + Hibernate的+ MSYQL應用。 我想將hibernate 4.3.11升級到最新版本的hibernate v5。

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

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

但是當我試圖訪問一個實體至極表是不是在默認模式,我有一個錯誤。 升級之前,它很好。 訪問一個實體,這個表在默認模式下適用於V5休眠。

默認模式是「角色」,但在我的應用程序的所有實體中,我在@table JPA Annotation中顯式使用模式。 即使架構是「角色」

實體:

@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() {} 

... 

} 

當我嘗試用該實體的讀訪問,我有Java錯誤:

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_ 

當我試圖堅持該實體,我在相同的主題,但在hibernate_sequence表中有一個錯誤

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. 

所以我試圖在我的問題之前找到解決方案。 我在休眠站點發現版本v5.0.8是穩定的,並嘗試過它。 我有同樣的問題。

所以我讀了V5.0.8的演變。 我唯一關注的變化是:命名策略 我修改了我的xml彈簧配置,並在網絡上找到了一些解決方案。 但它不起作用。

xml spring配置的提取物

<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> 

感謝您的幫助。

+0

這與[此問題]有關(https://stackoverflow.com/questions/11184025/what-are-the-jpa-table-annotation-catalog- and-schema-variables-used-for),但可能不是真的重複 – avalancha

回答

3

所以我發現了這個問題。

因爲Hibernate的5版本(不知道爲什麼,什麼),如果你訪問一個數據庫MySQL的幾個方案,這個註釋是不正確的了:

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

你必須完成的註解與參數catalog

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

有了這個補充,應用程序運行。有關目錄屬性的更多信息可參見hibernate user guide