【问题标题】:How to properly map the DTOs in Hibernate so that SessionFactory initializes properly?如何正确映射 Hibernate 中的 DTO,以便 SessionFactory 正确初始化?
【发布时间】:2014-11-05 01:13:01
【问题描述】:

这可能是一个非常基本的问题,但我尝试了很多事情,但都遇到了问题。爸爸们请忽略。

案例 1:

当我在 hibernate.cfg.xml 中提到我的映射时,如下所示:

<mapping class="com.tm.midservice.db.dto.User"/>
<mapping class="com.tm.midservice.db.dto.Company"/>

我收到以下错误。

INFO  - org.hibernate.tool.hbm2ddl.TableMetadata - table found: demodb.company
INFO  - org.hibernate.tool.hbm2ddl.TableMetadata - columns: [analytics_feature_enabled,     source_domain, currency, id, updated_at, source, token, name, domain, created_at, active, mid]
SessionFactory initial creation error.org.hibernate.HibernateException: Wrong column type: active, expected: bit`

我在 DTO 中描述的列是:

@Column(name = "active")
private boolean active;

(编辑 1)

尝试了以下方法,但没有奏效:

@Type(type="true_false")
@Column(name="active", columnDefinition = "TINYINT(1) DEFAULT 0")
private boolean active;

给出例外:

SessionFactory initial creation error.org.hibernate.HibernateException: Wrong column type: active, expected: TINYINT(1) DEFAULT 0

(编辑 2)

"Found: bit, expected: boolean" after Hibernate 4 upgrade找到了一些东西

@Column(name="active", columnDefinition = "BIT", length = 1)
private boolean active;

给出例外:

SessionFactory initial creation error.org.hibernate.HibernateException: Wrong column type: active, expected: BIT

(编辑 3)

试了一下:

public class Mysql5BitBooleanDialect extends MySQL5Dialect {
  public Mysql5BitBooleanDialect() {
    super();
    registerColumnType( Types.BOOLEAN, "bit" );
  }
}

这个类确实被调用了,但是异常没有发生。

案例 2

当我没有在 cfg 文件中提供映射并尝试运行我的应用程序时,sessionFactory 会初始化,但当我尝试执行任何数据库操作时,我会收到以下错误:

hibernate mapping exception unknown entity

理想情况下,我不应该提供任何映射定义,因为 DTO 已经获得了 [@Entity &amp; @Table (name = "User")] 的注释。

如何正确配置?我在这里缺少什么吗?我被困住了。

【问题讨论】:

  • 存储活动字段的列类型错误。 Hibernate 期望类型为bit 类型(即1 或0)。这是大多数数据库表示布尔值的方式
  • 你能参考我最新的编辑吗?尝试使用 active 字段但没有用。
  • 列定义不会做任何事情,你需要一个@Type 注释。这将改变 Hibernate 的预期
  • 在这里做什么?这件事进展如何?

标签: java hibernate


【解决方案1】:

问题出在数据库表上,我完全忽略了它。 TinyInt 字段的大小为 11,将其更改为 1 即可解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2022-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多