How do I specify integers to be of a certain length via a JPA annotation

user339108 picture user339108 · Aug 1, 2011 · Viewed 10.4k times · Source
create table foo (id INT(10) not null ...)

Instead of declaring id as "INTEGER", I would like to specify a length restriction as mentioned above.

I use

@Entity class Foo {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
private Integer id;

... }

How should I specify the length restriction in my JPA model class, so that hbm2ddl automatically generates the schema as per my requirement.

Answer

Piraba picture Piraba · Aug 1, 2011
 @Id
 @GeneratedValue(strategy = IDENTITY)
 @Column(name = "id",length=50, unique = true, nullable = false, insertable = false, updatable = false)
 private Integer id;

In your case you are use annotaion/JPA. If you used mapping then use like this :

<property name="id" type="Integer" length="20"/>

try this one