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.
2 Answers 2
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMP_ID", insertable = false)
private Integer empId;
1 Comment
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;
Comments
Explore related questions
See similar questions with these tags.
@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?