Acewin
Acewin

Reputation: 1727

Handling field of Integer with specific count of digits in entity

I am generating entity for a table having a integer field. In the database the field is set to allow 4 digits.

CREATE TABLE REG_DETAIL (
    REG_ID INTEGER (4) DEFAULT J NOT NULL,
    ACTV_CD CHAR    (1) DEFAULT 'N' NOT NULL,
    STATUSCD CHAR    (5) DEFAULT 'N' NOT NULL,
    DATE_CMPLETED DATE    (4) DEFAULT Y,
)

I am generating the entities using Data Explorer in Eclipse.

Generating through Data Explorer does not add any constraint on the integer.

What I would like to know if in JPA 2.0/Hibernate

  1. we can have a range added on the integer value accepted in an integer field.

  2. or if the number of digits allowed for the integer field can be controlled.

Alternatively if it is different in JPA 2.1

---UPDATE---

I was pointed to the Hibernate documentation. Hibernate provides annotations, Range, Length, Max, Min etc which helps to control the field values

Upvotes: 1

Views: 2440

Answers (1)

Elias Garcia
Elias Garcia

Reputation: 7282

When yo use @Column annotation, you can pass a length attribute to it to indicate the length of the field in the database.

@Column(name = "column_name", length = 10)

For example, this would create an INTEGER(10).

Also, you can use Bean Validation to specify max and min values with @Max and @Min annotations.

You can see all the possible options here.

Upvotes: 2

Related Questions