Hibernate 5 with JPA:hibernate访问,没有在实体中进行spécified

问题描述 投票:4回答:1

(对不起我的英语,这不是我的自然语言)

所以,我有一个MAVEN + SPRING + JPA + HIBERNATE + MYSQL应用程序。我想将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.

所以我试着在我的问题之前找到解决方案。我在hibernate网站上发现版本v5.0.8稳定并尝试过。我遇到了同样的问题。

所以我读了V5.0.8的演变。唯一引起我注意的变化是:命名策略我修改了我的xml spring配置,并在网上找到了一些解决方案。但是,它不起作用。

提取我的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>

谢谢你的帮助。

java spring hibernate jpa
1个回答
3
投票

所以我发现了问题。

从hibernate的第5版开始(不知道为什么和什么),如果你使用多个模式访问数据库MySQL,这个注释就不再正确了:

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

您必须使用参数catalog完成注释

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

有了这个补充,应用程序就会运行。有关目录属性的更多信息,请访问hibernate user guide

© www.soinside.com 2019 - 2024. All rights reserved.