How map a bit type in Mysql to hibernate?
19,419
Solution 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:
Solution 2
I had a similar problem. The following mapping in Java solved my problem:
@Column(name = "columnName", columnDefinition="BIT")
private Boolean columnVariable;
Solution 3
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;
Related videos on Youtube
Author by
Valter Silva
Updated on June 04, 2022Comments
-
Valter Silva almost 2 years
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.
-
Valter Silva about 13 yearsi would like to change that in the MySql, but in not the DBA so i think is better i change in hibernate then ask to change in database. i will try your approach.
-
jpfreire over 7 yearsBoolean... a dangerous wrapper: defined as null if not initialized. Had lots of trouble with some poorly tested code that was throwing exception in production environment.