“发现:位,预期:布尔” Hibernate 4升级后

发布于 2024-12-23 10:58:42 字数 1658 浏览 0 评论 0原文

我正在尝试从 Hibernate 3.6.5 升级到 4.0(以及从 Spring 3.0.5 升级到 Hibernate 4 支持所需的 3.1)。

现在,使用 MySQL 和 HSQL,我遇到了持久布尔字段的问题:

Caused by: org.hibernate.HibernateException: 
Wrong column type in PUBLIC.PUBLIC.EVENT for column Checked. Found: bit, expected: boolean
    at org.hibernate.mapping.Table.validateColumns(Table.java:282)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1268)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)

JPA @Entity@Column 注释用于域中对象,有问题的字段如下所示:

@Column(name = "Checked")
private boolean checked;

HSQL schema:

Checked bit default 0 not null,

MySQL schema:

`Checked` tinyint(1) NOT NULL default '0',

在坚持使用 Hibernate 4 的同时解决此问题的最直接方法是什么?我应该更改数据库架构、Hibernate 配置或域类注释吗?

我不知道之前的代码和配置是否完全“正确”,但至少它在 Hibernate 3 上工作得很好。

I'm trying to upgrade from Hibernate 3.6.5 to 4.0 (and from Spring 3.0.5 to 3.1 which is required for Hibernate 4 support).

Now, with both MySQL and HSQL, I'm running into this problem with persistent boolean fields:

Caused by: org.hibernate.HibernateException: 
Wrong column type in PUBLIC.PUBLIC.EVENT for column Checked. Found: bit, expected: boolean
    at org.hibernate.mapping.Table.validateColumns(Table.java:282)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1268)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)

JPA @Entity and @Column annotations are used in domain objects, and the problematic fields look like this:

@Column(name = "Checked")
private boolean checked;

HSQL schema:

Checked bit default 0 not null,

MySQL schema:

`Checked` tinyint(1) NOT NULL default '0',

What is the most straightforward way to solve this while sticking with Hibernate 4? Should I change the database schema, Hibernate configs, or domain class annotations?

I have no idea if the code and configuration was fully "correct" before, but at least it worked fine with Hibernate 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜扑 2024-12-30 10:58:42

我通过将 columnDefinition = "BIT" 添加到 @Column 行来解决这个问题。

@Basic
@Column(name = "B", columnDefinition = "BIT", length = 1)
public boolean isB() {
    return b;
}

它在数据库中也定义为“BIT(1)”。还与 TINYINT 合作过。这是我发现的最简单的解决方案,因为更改非常小并且不需要接触数据库。

使用:MySQL服务器5.5.13,Hibernate 4.1.1,JDK 1.6

I worked this out by adding columnDefinition = "BIT" to the @Column line.

@Basic
@Column(name = "B", columnDefinition = "BIT", length = 1)
public boolean isB() {
    return b;
}

Its defined as a 'BIT(1)' in the DB as well. Also worked with TINYINT. This is the easiest solution I've found since the change is super-minor and no need to touch the DB.

Using: MySQL Server 5.5.13, Hibernate 4.1.1, JDK 1.6

伴梦长久 2024-12-30 10:58:42

我遇到了同样的问题,我扩展了方言以考虑到 mysql 将 boolean 视为 bit 的别名这一事实。

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

我不使用更长的 bit() 字段(例如表示 byte[]),因此这可能会破坏这一点。

I had the same problem and I extended the Dialect to take into account the fact that mysql treats boolean as an alias of bit.

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

I don't use longer bit() fields (to represent for example byte[]) so this might break that.

二智少女 2024-12-30 10:58:42

我通过将 transformedBitIsBoolean=true 添加到我的 MySQL 连接字符串来解决这个问题。请参阅此处:https://hibernate.atlassian.net/browse/HHH-6935

I was able to solve this issue by adding transformedBitIsBoolean=true to my MySQL connection string. See here: https://hibernate.atlassian.net/browse/HHH-6935

夜司空 2024-12-30 10:58:42

这已经在类似的问题中得到了回答:

Hibernate JPA、MySQL 和 TinyInt(1) 用于布尔值而不是位或字符

您的问题可能是当你同时使用 HSQL DB 时会稍微复杂一些,但你还是可以看看并尝试一下!

This has been answered in a similar question here :

Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

Your problem could be a bit more complicated as you use HSQL DB at the same time, but you can take a look and try it anyway !

柳若烟 2024-12-30 10:58:42

发现问题了
我还得到了 org.hibernate.HibernateException: 错误的列类型 ... 发现: 位,预期:

hibernate 4 中 BooleanType 上的 boolean 他们将 Ctor 更改为

public BooleanType() {
    this( org.hibernate.type.descriptor.sql.BooleanTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}

而不是旧版本

public BooleanType() {
    this( BitTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}

Found the problem
i also got org.hibernate.HibernateException: Wrong column type ... Found: bit, expected: boolean

on BooleanType in hibernate 4 they changed the Ctor to

public BooleanType() {
    this( org.hibernate.type.descriptor.sql.BooleanTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}

instead of old versions

public BooleanType() {
    this( BitTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文