如何将Mysql中的位类型映射到hibernate?

发布于 2024-10-30 13:04:50 字数 723 浏览 1 评论 0原文

我在班级中使用逆向工程并得到以下结果:

@Entity
@Table(name = "user", catalog = "bytecode", uniqueConstraints =
@UniqueConstraint(columnNames = "email"))
public class User implements java.io.Serializable {

    private Integer id;
    private String email;
    private String password;
    private boolean type;

数据库:

CREATE TABLE  `bytecode`.`user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `type` bit(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

但我不想在属性“类型”中设置“真”或“假”,而是设置 1 或 0。 我怎样才能在休眠状态下做到这一点?

此致, 瓦尔特·恩里克.

i use the reverse engeneering in my class and get this:

@Entity
@Table(name = "user", catalog = "bytecode", uniqueConstraints =
@UniqueConstraint(columnNames = "email"))
public class User implements java.io.Serializable {

    private Integer id;
    private String email;
    private String password;
    private boolean type;

Database:

CREATE TABLE  `bytecode`.`user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `type` bit(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

But i don't want to set 'true' or 'false' in my attribute 'type' but 1 or 0.
How can i do that in hibernate ?

Best regards,
Valter Henrique.

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

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

发布评论

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

评论(4

書生途 2024-11-06 13:04:50

Hibernate 有一个特殊的 numeric_boolean 类型用于这种映射。您可以按如下方式配置它:

@Type(type = "numeric_boolean")
private boolean type;  

另请参阅:

Hibernate has a special numeric_boolean type for this kind of mapping. You can configure it as follows:

@Type(type = "numeric_boolean")
private boolean type;  

See also:

忱杏 2024-11-06 13:04:50

我有类似的问题。 Java 中的以下映射解决了我的问题:

@Column(name = "columnName", columnDefinition="BIT")
private Boolean columnVariable;

I had a similar problem. The following mapping in Java solved my problem:

@Column(name = "columnName", columnDefinition="BIT")
private Boolean columnVariable;
流年里的时光 2024-11-06 13:04:50

MySQL 中是否必须将其作为 bit 类型?最简单的解决方案是将 MySQL 中的数据类型更改为 tinyint(1)

否则,您应该能够使用注释将实体类型映射到整数; 不确定这个,必须查一下

...
@Column(nullable=false)
@Type(type="org.hibernate.type.BooleanType")
private short type;

Do you have to have it as a bit type in MySQL? The easiest solution would be to change the data type in MySQL to tinyint(1).

Otherwise you should be able to map your entity type to an integer using annotations; Not sure about this, have to look it up

...
@Column(nullable=false)
@Type(type="org.hibernate.type.BooleanType")
private short type;
宛菡 2024-11-06 13:04:50

http://bugs.mysql.com/bug.php?id=28422表明这是一个错误。 http://www.xaprb.com/blog/ 2006/04/11/bit-values-in-mysql/ 建议跳过它是明智的。但是当然你不能告诉DBA不要在MySQL中使用位列——这意味着我们要么需要使用旧版本的MySQL(<5.0.3),要么根本不使用MySQL的位+ Hibernate。

http://bugs.mysql.com/bug.php?id=28422 suggests it is a bug. http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/ suggests it would be wise to skip it. But of course you can't tell the DBA to not use a bit column in MySQL - meaning either we need to use and older version of MySQL (< 5.0.3) or not use MySQL's bit + Hibernate at all.

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