Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Increment in hibernate using oracle

I am new to hibernate and I want to insert primary number in my table for unique identification. I am using Oracle as my database so do I need to create sequence in oracle to get auto increment generation number ?

I am using below code but it is not working. I have not created any sequence yet.

 @Id
 @Column(name = "id" )
 @GeneratedValue ( strategy = GenerationType.TABLE)

I have used AUTO, SEQUENCE and IDENTITY but nothing works for me.

like image 846
Pratik Joshi Avatar asked Jan 11 '23 01:01

Pratik Joshi


2 Answers

this is one way of using Oracle sequence in a JPA mapped entity:

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_NAME")
@SequenceGenerator(name = "SEQUENCE_NAME", sequenceName = "SEQUENCE_NAME", allocationSize = 1, initialValue = 1)

In this way your persist() method will ask for the next value of the sequence in order to use it as ID for your entry.

like image 136
seph Avatar answered Jan 17 '23 05:01

seph


You can ue this @GeneratedValue(strategy=GenerationType.AUTO)

@Id
@Column(name = "id" )
@GeneratedValue(strategy=GenerationType.AUTO)
like image 37
Java_User Avatar answered Jan 17 '23 03:01

Java_User