To use the Oracle 12c Identity Generation, use at least Hibernate 5.3. Example:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.4.Final</version>
</dependency>
Your application.properties
will need to provide the Oracle12cDialect
.
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
spring.datasource.platform=oracle
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@hostname:1521:MYSID
spring.datasource.username=MY_USER
spring.datasource.password=$ecret
spring.jpa.hibernate.ddl-auto=none
Given a table with auto generated primary key:
CREATE TABLE MY_ENTITY (
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY
);
Use GenerationType.IDENTITY
as strategy on the JPA entity.
@Entity
@Table(name = "MY_ENTITY")
public class MyEntity {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// the rest of your properties
}
To check if it's working, update your auto generated sequence with something like:
ALTER TABLE MY_ENTITY MODIFY (ID GENERATED AS IDENTITY START WITH 10000);
When inserting new entries, their ID's should be like 10001
, 10002
, etc.