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

时间:2022-07-01 16:24:25

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 ?

但是我不想在我的属性'type'中设置'true'或'false'但是1或0.我怎样才能在hibernate中做到这一点?

Best regards, Valter Henrique.

此致,Valter Henrique。

4 个解决方案

#1


6  

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

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

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

See also:

#2


2  

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).

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

#3


2  

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

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

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

#4


0  

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.

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的bit + Hibernate。

#1


6  

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

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

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

See also:

#2


2  

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).

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

#3


2  

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

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

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

#4


0  

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.

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的bit + Hibernate。