JPA: Override Automatically Generated Identifier

I have the following definition in the Employee class

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employee_id")
private Integer employeeId;

Now I want to import existing employees with existing employee identifiers. Even if I set the employee identifier before saving, the assigned identifier is ignored and the ID automatically increases. How do we redefine this?

+5
source share
4 answers

I found a duplicate thread with the same problem. bypass the generated value in sleep mode

So finally, I wrote my own generator to solve the problem.

Thank you all for your help :)

+2
source

. JPA . , GenerationType.AUTO , SQL.

.

+1

@MappedSuperclass

@MappedSuperclass
public AbstractEmployee {
   // declare all properties, getters, setters except for the id
}

@Entity
@Table(name="EMPLOYEE")
public class Employee extends AbstractEmployee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "employee_id")
    private Integer employeeId;

    ...
}

@Entity
@Table(name="EMPLOYEE")
public class EmployeeWithAssignedId extends AbstractEmployee {
    @Id
    @Column(name = "employee_id")
    private Integer employeeId;

    ...
}

, , . , , . , JPA , :)

0

you must override the getEmployeeId () method and use the annotation of this method in this case JPA uses getter methods for all fields in the class, and you must move the other Annotaions methods in getter methods

0
source

All Articles