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
  • 19,824
  • 17
  • 99
  • 186
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • `@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? – João Dias Aug 17 '21 at 17:38
  • https://stackoverflow.com/questions/57767571/how-do-i-use-oracle-12c-automatic-identity-generation-with-hibernate-spring-and/57767572 Does this solves your problem ? – Shawrup Aug 17 '21 at 19:01
  • 1
    Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Sep 07 '21 at 20:57

2 Answers2

1
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMP_ID", insertable = false)   
private Integer empId;
  • 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. – Lolly Aug 17 '21 at 17:56
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;
Lucia
  • 791
  • 1
  • 8
  • 17