【问题标题】:Hibernate schema generation: same configurations, different behaviourHibernate 模式生成:相同的配置,不同的行为
【发布时间】:2013-08-21 08:56:24
【问题描述】:

我正在做一个项目,遇到了一个非常莫名其妙的问题。

一些背景:该项目是一个相当标准的 Java 7 项目,使用 Maven 3.0.4 作为构建工具和 spring (3.2.3.RELEASE) 进行依赖注入。 Hibernate (4.1.12.Final) 用于持久化。

在运行与持久性相关的单元测试(使用 Spring 的 AbstractTransactionalJUnit4SpringContextTests)时,会在仅为测试创建的临时数据库中创建架构(使用 hibernate 的 hibernate.hbm2ddl.auto=update 属性)。

这一切在我的开发机器(Linux Mint 12)上运行良好。 然而,当在我的笔记本电脑上运行完全相同的代码时(最近安装了 Linux Mint 15),我发现 Hibernate 没有创建任何外键。

这特别奇怪,因为:

  • 我正在运行从头开始构建的完全相同的软件(相同的结帐)(已清除 .m2 存储库)
  • 使用完全相同的 Maven 版本 (3.0.4)
  • 具有相同的 Spring 和 Hibernate 版本(因为这些是在使用源代码签出的 maven 构建配置中配置的)
  • 使用几乎相同的 Postgresql 数据库(工作机器上的软件包版本 9.1.9-0ubuntu11.10,笔记本电脑上的软件包版本 9.1.9-1ubuntu1),它们具有完全相同的配置(除了区域设置)
  • 使用 postgres 超级用户访问数据库,因此权限不是问题

通常我很擅长找出任何奇怪问题的根源,但这绝对让我感到困惑。我看不出任何可能导致这种情况的相关差异...... 任何有关如何找到原因的线索将不胜感激!

我可以看到 2 个可能导致此问题的线索:

  • 我在笔记本电脑上摆弄了一下区域设置,这会以某种方式导致这个问题吗?
  • 在下面的 Hibernate 属性中,您可以看到使用了 PostgreSQL82Dialect。我找到了this Hibernate forum thread,其中提到了以下内容:

我看到您正在使用 ProgressDialect,但此方言不支持更改表(ProgressDialect.hasAlterTable() 返回 false)。

这似乎是导致此问题的最佳可能原因,但是我仍然无法理解为什么它适用于一种设置而不适用于另一种设置,因为两者的 Hibernate 配置是相同的......

一些额外信息:

Hibernate 配置(尽管这在两种设置上是相同的!):

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
    <property name="persistenceUnitManager" ref="jpaPersistenceUnitManager" />
    <property name="jpaDialect" ref="hibernateJpaDialect" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.cache.use_minimal_puts">false</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.jdbc.use_get_generated_keys">false</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache-unity-persistence.xml</prop>
        </props>
    </property>
</bean>

如果您认为有帮助,请随时索取更多信息! 或者,如果您想让我检查/尝试某些东西,请告诉我!

这真的让我很烦!

【问题讨论】:

  • 我不确定,但是检查日志怎么样?您将在那里获得有价值的信息
  • 你可以开启Hibernate日志看看有没有异常
  • 不知道。但是,也许您可​​以使用 ShemaExporter 将您的数据库更新导出到 .sql 文件并看到此问题仍然存在。如果是,您可以在 Hiberante 源代码上设置一些断点并尝试解决。

标签: java spring hibernate postgresql postgresql-9.1


【解决方案1】:

好的,来自 Hoàng Long 查看日志的提示让我得到了答案: 显然我的数据库刚刚损坏。

除了用于创建外键约束的 'ALTER TABLE' 语句之外,一切都会正常运行,这会因错误而失败。

显然我的 postgresql 安装搞砸了...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 2011-03-16
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多