如何将Mysql中的位类型映射到hibernate?
我在班级中使用逆向工程并得到以下结果:
@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;
数据库:
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;
但我不想在属性“类型”中设置“真”或“假”,而是设置 1 或 0。 我怎样才能在休眠状态下做到这一点?
此致, 瓦尔特·恩里克.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Hibernate 有一个特殊的
numeric_boolean
类型用于这种映射。您可以按如下方式配置它:另请参阅:
Hibernate has a special
numeric_boolean
type for this kind of mapping. You can configure it as follows:See also:
我有类似的问题。 Java 中的以下映射解决了我的问题:
I had a similar problem. The following mapping in Java solved my problem:
MySQL 中是否必须将其作为
bit
类型?最简单的解决方案是将 MySQL 中的数据类型更改为tinyint(1)
。否则,您应该能够使用注释将实体类型映射到整数;不确定这个,必须查一下Do you have to have it as a
bit
type in MySQL? The easiest solution would be to change the data type in MySQL totinyint(1)
.Otherwise you should be able to map your entity type to an integer using annotations;Not sure about this, have to look it uphttp://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的位+ Hibernate。
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.