my api used spring 5 version and product entity has a two many to one relations with productsupplier and category and i have to mention that I use jpa for crud operations. I have no problem for posing a product object with two dependency's but problem happens when i post again another object , the next product id value doesn't start in the correct order and next id is 3 times bigger that the last one like(1,4,7,10) instead of the (1,2,3,4) and this makes a problem when my api is in the production and i was wondering how can i solve it
product
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne( cascade = CascadeType.ALL)
@JoinColumn
private Category category;
private int price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn
private ProductSupplier productSupplier;
//setters and getters
category
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String CategoryName;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL,mappedBy = "category")
@JsonBackReference
private List<Product> products;
//setters and getters
productSuplier
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@JsonBackReference
@OneToMany(mappedBy="productSupplier",fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Product> product;
//setters and getters
post method
@PostMapping
@ResponseStatus(HttpStatus.ACCEPTED)
public ResponseEntity<Product> postProduct(@RequestBody Product product){
productRepository.save(product);
return ResponseEntity.ok(product) ;
}
2 objects that i posted
[
{
"id": 1,
"name": " micro type charger",
"category": {
"id": 2,
"categoryName": "phone"
},
"price": 0,
"productSupplier": {
"id": 3,
"name": "samsung"
}
},
{
"id": 4, //id of new product must be 2 not 4
"name": " micro type charger",
"category": {
"id": 5,
"categoryName": "phone"
},
"price": 0,
"productSupplier": {
"id": 6,
"name": "samsung"
}
}
question from:
https://stackoverflow.com/questions/65890288/spring-rest-doesnt-set-a-id-value-in-correct-order 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…