java - 带有 JPA : hibernate access without schema spécified in entity 的 Hibernate 5

标签 java spring hibernate 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 hibernate 。

默认模式是“角色”,但在我的应用程序的所有实体中,我在@table JPA 注释中明确了模式。 即使模式是“角色”

实体:

@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": []

在hibernate的日志中,我看到SQL命令不包含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": []

在hibernate的日志中,SQL命令不包含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>

最佳答案

所以我发现了问题。

从 hibernate 版本 5 开始(不知道为什么和什么),如果您访问具有多个模式的数据库 MySQL,此注释不再正确:

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

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

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

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

关于java - 带有 JPA : hibernate access without schema spécified in entity 的 Hibernate 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35864745/

相关文章:

java - 无法解析 Eclipse 中的 Neo4j 注释

java - 如果 react 中包含空格,则无法搜索名称

hibernate - 使用Tomcat 7时hibernate的配置文件放在哪里?

java - 从正在运行的任务中关闭 ExecutorService

java.lang.UnsupportedOperationException : 'posix:permissions' not supported as initial attribute on Windows 异常

java - spring.session.timeout 与 server.servlet.session.timeout

java - Hibernate中如何设置外键实体

java - 为什么在 Oracle 上使用 Hibernate 批量插入数据对于较大的事务要慢得多?

java - 添加模块描述符时没有名为 'entityManagerFactory' 的 bean 可用

java - 如何使用 JFileChooser 将 CSV 文件数据输入到 JTable 中