简体   繁体   English

如何在Hibernate注释中创建Tinyint Field

[英]How to create Tinyint Field in Hibernate annotation

I have problem in creating tinyint field in MySQL database using Hibernate. 我在使用Hibernate在MySQL数据库中创建tinyint字段时遇到问题。

I used to write the Entity class like this 我曾经写过像这样的Entity类

@Entity @实体

@Table(name="tablename") @table(名称= “表名”)

public class MyEntity { 公共课MyEntity {

private int id;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

} }

And when I check MySQL, id field always end up with Integer(11) , how do I make it as TinyInt ? 当我检查MySQL时,id字段总是以Integer(11)结束,我如何将其作为TinyInt?

Any answer would be appreciated 任何答案将不胜感激

I think there are a few ways of doing this. 我认为有几种方法可以做到这一点。 The first is to use the columnDefinition attribute on the @Column annotation to read something like this: 第一种是使用@Column注释上的columnDefinition属性来读取如下内容:

@Column(name = "id", columnDefinition = "TINYINT")

This is not portable across databases and does not align well with the variable itself being an int. 这在数据库中是不可移植的,并且与变量本身作为int不能很好地对齐。 A byte or short may be needed here 这里可能需要一个字节或一个字节

Another way is to use the @Type annotation, probably with org.hibernate.type.ByteType since it seems to represent a TINYINT ( link to javadoc ). 另一种方法是使用@Type注释,可能使用org.hibernate.type.ByteType,因为它似乎代表了一个TINYINT( 链接到javadoc )。

I hope this helps. 我希望这有帮助。

如果从bean中的注释创建模式,则可以尝试使用@Column批注中的columnDefinition属性来定义要执行的SQL。

将int更改为byte或Byte,无需其他注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM