@Entity public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; }
The table “STUDENT” generated:
Specify table name, column name, nullable, length and unique:
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME", nullable=false, unique=true, length=30) private String name; }
Constraints:
CREATE UNIQUE INDEX SQL110902022422451 ON T_STUDENT(STU_NAME);
ColumnDefinition overwrites length
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME", nullable=false, unique=true, columnDefinition="VARCHAR(50)", length=30) private String name; }
The table “T_STUDENT” generated:
Integer column and BigDecimal column
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME") private String name; @Column(name="AGE") private Integer age; @Column(name="MONEY") private BigDecimal money; }
The table “T_STUDENT” generated:
Specify precision and scale of BigDecimal
(The number 123.45 has a precision of 5 and a scale of 2)
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME") private String name; @Column(name="AGE") private Integer age; @Column(name="MONEY", precision=5, scale=2) private BigDecimal money; }
The table “T_STUDENT” generated:
Date column
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME") private String name; @Column(name="AGE") private Integer age; @Column(name="MONEY", precision=5, scale=2) private BigDecimal money; @Column(name="START_DATE") private Date start; }
The table “T_STUDENT” generated:
Temporal annotation to specify the date type (date, time, timestamp)
@Column(name="START_DATE") @Temporal(TemporalType.DATE) private Date start;
The column “START_DATE” generated:
The value:
@Column(name="START_DATE") @Temporal(TemporalType.TIME) private Date start;
The column “START_DATE” generated:
The value:
@Column(name="START_DATE") @Temporal(TemporalType.TIMESTAMP) //This is the default private Date start;
The column “START_DATE” generated:
The value:
Specify default date, not insertable, not updateable
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME") private String name; @Column(name="START_DATE", columnDefinition="DATE DEFAULT CURRENT_DATE", insertable=false, updatable=false) private Date start; }
The table “T_STUDENT” generated:
Blob and Clob
@Entity @Table(name="T_STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="STU_ID") private int id; @Column(name="STU_NAME") private String name; @Lob @Column(name="REPORT") private String report; @Lob @Column(name="IMAGE") private byte[] image; }
The table “T_STUDENT” generated: