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

Val*_*lva 9 java mysql jsp hibernate servlets

我在班上使用逆向工程并得到这个:

@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;
Run Code Online (Sandbox Code Playgroud)

数据库:

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;
Run Code Online (Sandbox Code Playgroud)

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

此致,Valter Henrique.

axt*_*avt 8

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

@Type(type = "numeric_boolean")
private boolean type;  
Run Code Online (Sandbox Code Playgroud)

也可以看看:


Bjö*_*örn 2

你必须将它作为bitMySQL 中的一种类型吗?最简单的解决方案是将 MySQL 中的数据类型更改为tinyint(1).

否则,您应该能够使用注释将实体类型映射到整数;这个不太清楚,得查一下

...
@Column(nullable=false)
@Type(type="org.hibernate.type.BooleanType")
private short type;
Run Code Online (Sandbox Code Playgroud)