【问题标题】:Create table using Hibernate JPA for Oracle throws error使用 Hibernate JPA for Oracle 创建表会引发错误
【发布时间】:2013-07-18 07:29:13
【问题描述】:

我尝试通过将 hibernate.hbm2ddl.auto 设置为“创建”来使用 JPA 自动创建 Oracle 表。 实体字段之一是布尔类型,在 Java 中如下所示:

private boolean activateCustomer;

但是,当我们运行它时,会抛出一个错误,当我查看错误时,生成的 sql 类型是布尔值而不是 Number(1)。我使用 Hibernate 4.1.9 和 Oracle XE(文件:OracleXE112_Win32.zip)。

根据我在互联网上找到的信息(大多数是几年前),默认情况下它应该是 Number(1)。是否对 Oracle、Hibernate 或 JPA 进行了任何更改以改变行为?

或者,有没有我遗漏的配置?谢谢。


JPA 配置:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
        <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
        <!-- Uncomment the following two properties for JBoss only -->
        <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
        <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
    </properties>
</persistence-unit>

【问题讨论】:

  • 您是否为 Hibernate 选择了 Oracle 方言?
  • 你能发布你的休眠配置文件吗?
  • 嗨,没有休眠配置,只有 JPA 配置。我只是附上它。它最初是由 Spring Roo 生产的
  • 我最终会这样做:@Column(name = "activate_customer", columnDefinition = "NUMBER(1)") private Boolean activateCustomer;这可能不是最好的解决方案,但它确实有效。我认为最好的是 Hibernate JPA 是否可以在没有任何额外注释的情况下生成正确的 DDL,因为它知道它是针对 Oracle 的。
  • 看起来您使用的是 11g,您的方言不应该是 'org.hibernate.dialect.Oracle10gDialect' 吗? 'org.hibernate.dialect.OracleDialect' 适用于 Oracle 8。

标签: java oracle hibernate jpa


【解决方案1】:

看起来您使用的是 11g,所以您的方言应该是 org.hibernate.dialect.Oracle10gDialect 而不是仅适用于 Oracle 8 的 org.hibernate.dialect.OracleDialect

【讨论】:

    【解决方案2】:

    尝试使用:

    @Column(columnDefinition = "TINYINT")
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private boolean activateCustomer;
    

    【讨论】:

    • 这将产生数据库字段“activate_customer TINYINT”并产生错误:“ORA-00902:无效数据类型(0 行受影响)”
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2020-05-17
    • 1970-01-01
    • 2021-01-28
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多