JPA Insert + Oracle Sequences example
By:Roy.LiuLast updated:2019-08-11
A quick JPA + Oracle sequences example, for self reference.
1. Oracle Database
Issue the following SQL script to create a table and a sequence.
CREATE TABLE CUSTOMER( ID NUMBER(10) NOT NULL, NAME VARCHAR2(100) NOT NULL, EMAIL VARCHAR2(100) NOT NULL, CREATED_DATE DATE NOT NULL, CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID) ); CREATE SEQUENCE customer_seq MINVALUE 1 MAXVALUE 9999999999 START WITH 4 INCREMENT BY 1;
2. JPA
In Java, add JPA annotations like this :
Customer.java
package com.mkyong.model; import javax.persistence.*; import java.util.Date; @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUST_SEQ") @SequenceGenerator(sequenceName = "customer_seq", allocationSize = 1, name = "CUST_SEQ") Long id; String name; String email; @Column(name = "CREATED_DATE") Date date; //...
References
From:一号门
COMMENTS