0

I am using SpringBoot2 for creating microservice. Database table I am using for entity is been defined with default sequence at DB level for the primary key. Like below,

CREATE TABLE EMPLOYEE (
EMP_ID INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1 .....) NOT NULL,
EMP_NAME VARCHAR2(100 BYTE))

I want the spring boot data jpa to use the above default sequence for emp id

@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@Column(name = "EMP_ID", insertable = false) 
private Integer empId;
@Column(name = "EMP_NAME", insertable = false) 
private String empName
}

I want the empId to be generated from the default sequence added in Create Table query. I tried all the strategy in GeneratedValue but it is not working. I am only left with creating explicit sequence and use it in sequence generator.

halfer
20.2k19 gold badges110 silver badges207 bronze badges
asked Aug 17, 2021 at 17:26
2
  • @GeneratedValue(strategy = GenerationType.IDENTITY) should do the trick since it relies on an auto-incremented database column and lets the database generate a new value with each insert operation. But if you tried all the strategies and it didn't work that is in fact weird. What did you get when trying this strategy? Commented Aug 17, 2021 at 17:38
  • stackoverflow.com/questions/57767571/… Does this solves your problem ? Commented Aug 17, 2021 at 19:01

2 Answers 2

1
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMP_ID", insertable = false) 
private Integer empId;
answered Aug 17, 2021 at 17:42
Sign up to request clarification or add additional context in comments.

1 Comment

Tried with AUTO strategy and hibernate tries to execute - select hibernate_sequence.nextval from dual and throws error that hibernate_sequence does not exists instead of getting it from default sequence.
0

Please try below thing, it should work as expected by you.

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(nullable = false, columnDefinition = "INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL")
 private Integer empId;
answered Aug 18, 2021 at 7:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.