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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With