java 如何将 Mysql 中的位类型映射到休眠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5567322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:41:53  来源:igfitidea点击:

How map a bit type in Mysql to hibernate?

javamysqlhibernatejspservlets

提问by Valter Silva

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 ?

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

Best regards, Valter Henrique.

最好的问候,瓦尔特·恩里克。

回答by axtavt

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

Hibernatenumeric_boolean为这种映射提供了一种特殊类型。您可以按如下方式配置它:

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

See also:

也可以看看:

回答by Bj?rn

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

你必须把它作为bitMySQL 的一个类型吗?最简单的解决方案是将 MySQL 中的数据类型更改为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;

回答by Divya Rakshu

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

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

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

回答by Mukus

http://bugs.mysql.com/bug.php?id=28422suggests 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.

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