I´m refactoring a system to use Spring Boot/JPA/Hibernate. There is a routine where the clients instances receive a list of objects from the central server to save. Each object from the list has an ID generated by the central server and I have to use the same ID when inserting on my clients instances, but this ID on my entity is an Autoincrement field.
My Entity:
@Entity
@Table(name = "usuario", schema = "adm")
@SequenceGenerator(name="seq_usuario_generator", sequenceName = "adm.seq_usuario", allocationSize=1)
public class UsuarioEntity implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_usuario_generator")
@Column(name = "id_usuario", unique = true, nullable = false)
private int id;
@Column(name = "name", length = 50)
private String name;
@Column(name = "active")
private Boolean active;
// getter and setter ommited
....
}
Should have something like this:
...
UsuarioEntity user = new UsuarioEntity();
user.setId(idFromCentralServer);
user.setName("Testing insert with given ID");
usuarioRepo.save(user);
...
When I execute this code, the given ID is ignored and a new one is generated from the local sequence.
Is there any way to save an object where I can set an ID without getting it from my sequence?
I still need to have the autoincrement feature, when the given ID is not provided.