views:

387

answers:

2

hi all, 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

A: 

you can try using the columnDefinition property in the @Column annotation to define the SQL you want to execute if you create your schema from the annotations in your beans.

kpolice
A: 

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.

Gennadiy
Thank you, It worksusing your way, - first i create My own Mysql dialect, extends from org.hibernate.dialect.MySQLDialectand then I addregisterColumnType(Types.TINYINT, "TINYINT");and then I add columnDefinition = "TINYINT" in @Column annotationthank you very much, I appreciated that
Herry