JPA插入+ Oracle序列示例

一个快速的JPA + Oracle序列示例,仅供参考。

1. Oracle数据库

发出以下SQL脚本以创建表和序列。

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

在Java中,添加JPA注释,如下所示:

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;

    //...
}

下载源代码

下载– spring-data-jpa-insert-oracle-example.zip (6 KB)

参考文献

  1. Oracle / PLSQL:序列(自动编号)
  2. Spring Boot + Spring Data JPA + Oracle示例

翻译自: https://mkyong.com/jpa/jpa-insert-oracle-sequences-example/

你可能感兴趣的:(JPA插入+ Oracle序列示例)