How map a bit type in Mysql to hibernate?

。_饼干妹妹 提交于 2020-04-09 19:02:33

问题


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.


回答1:


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:

  • 6.1.1. Basic value types



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

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:


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

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



回答4:


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.



来源:https://stackoverflow.com/questions/5567322/how-map-a-bit-type-in-mysql-to-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!