Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Tinyint Field in Hibernate annotation

Tags:

I have problem in creating tinyint field in MySQL database using Hibernate.

I used to write the Entity class like this

@Entity

@Table(name="tablename")

public class 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 ?

Any answer would be appreciated

like image 772
Herry Avatar asked Dec 08 '09 04:12

Herry


1 Answers

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(name = "id", columnDefinition = "TINYINT")

This is not portable across databases and does not align well with the variable itself being an 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).

I hope this helps.

like image 170
Gennadiy Avatar answered Nov 15 '22 06:11

Gennadiy