Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
52 views
in Technique[技术] by (71.8m points)

java - spring rest doesn't set a id value in correct order

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hibernate uses its default db sequence to generate primary key values, if you don't provide any custom sequence name from your database.

In your case, Hibernate using its default db sequence because you didn't provide any custom sequence name. Whenever you're persisting a new product object in the database, two more objects of category and productSupplier are also getting persisted parallely. That's why, id generated for product is 1, then id for category will be generated as 2 and id for productSupplier will be generated as 3.

And When you're persisiting 2nd product object, counter of hibernate sequence is at 4 now. Hence, its showing 4 instead of 2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...