'A correct way to link one to many bidirectional relationship with Hibernate?

I'm using two Entities with a One to Many Bidirectional relationship between them and trying to make a HTTP POST Request, to link these two, problem is, the post is processed but DB i have null instead of foreign id of the entity

Category Entity:

@Entity
public class Category
{
@Id @GeneratedValue private Long categoryId;

    //search category for this product
    //search products for this category
    //-> bidirectional
    @OneToMany(mappedBy = "productCategory", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, orphanRemoval = true)
    @org.hibernate.annotations.OnDelete(action = OnDeleteAction.CASCADE)
    @OrderColumn(name = "PRODUCT_POSITION_ON_RAFT", nullable = false) // at fetching time
    private Collection<Product> productCollection = new ArrayList<>();

//getters, setters, no arg constructor

//link these entities together
    public void addProduct(Product product)
    {
        this.productCollection.add(product);
        product.setProductCategory(this);
    }

Product Entity

    public class Product {
    
    @Id @GeneratedValue private Long productId;
    
       @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
        @JoinColumn(name = "PRODUCT_CATEGORY_ID")
        @NotNull
        private Category productCategory;

//getters setters, no arg constructor

So the HTTP Post request is like this: -> for {categoryName} save that {productName} in Category#ProductCollection

@PostMapping("/addProducts/{categoryName}/{productName}")
    public ResponseEntity<Category> addProductsForThisCategory(@PathVariable ("categoryName") String categoryName, @PathVariable String productName)
    {
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(categoryService.addProductForThisCategory(categoryName, productName));
    }

-> going here, category is saved, i make sure Product for that productName exists before make the request

public Category addProductForThisCategory(String categoryName, String productName) {

    Category category = getCategoryByName(categoryName);
    ResponseEntity<Product> product = callingFromCategoryTo.callProduct(productName);
    category.addProduct(product.getBody());
    categoryRepository.save(category);
    return category;

}

-> Category Entity is saved, Product Entity is saved with null at foreign key who links Category what to do?

EDITED: Sql Logs:

Create Category: Hibernate: insert into PROJECT_HIBERNATE_Category (CATEGORY_DESCRIPTION, CATEGORY_NAME, CATEGORY_ID) values (?, ?, ?)

Create Product: Hibernate: insert into PROJECT_HIBERNATE_Product (PRODUCT_DESCRIPTION, PRODUCT_NAME, PURCHASED_AT_, PRODUCT_QUANTITY, PRODUCT_ID) values (?, ?, ?, ?, ?)

Make Links together:

Hibernate: select category0_.CATEGORY_ID as category1_0_, category0_.CATEGORY_DESCRIPTION as category2_0_, category0_.CATEGORY_NAME as category3_0_, category0_.PROMOTION_ID as promotio4_0_ from PROJECT_HIBERNATE_Category category0_ where category0_.CATEGORY_NAME=?
Hibernate: select product0_.PRODUCT_ID as product_1_5_, product0_.CUSTOMER_ID as customer8_5_, product0_.PRODUCT_CATEGORY_ID as product_9_5_, product0_.PRODUCT_DESCRIPTION as product_2_5_, product0_.PRODUCT_NAME as product_3_5_, product0_.PRODUCT_EXPIRES_AT_ as product_4_5_, product0_.PURCHASED_AT_ as purchase5_5_, product0_.PRODUCT_QUANTITY as product_6_5_, product0_.PRODUCT_STATUS as product_7_5_, product0_.PROMOTION_ID as promoti10_5_ from PROJECT_HIBERNATE_Product product0_ where product0_.PRODUCT_NAME=?
Hibernate: select pricecolle0_.PRODUCT_ID as product_3_4_0_, pricecolle0_.PRICE_ID as price_id1_4_0_, pricecolle0_.PRICE_ID as price_id1_4_1_, pricecolle0_.PRICE_AMOUNT as price_am2_4_1_, pricecolle0_.PRODUCT_ID as product_3_4_1_, pricecolle0_1_.CURRENCY_EUROPE as currency1_2_1_, pricecolle0_1_.SHIPPING_COSTS_IN_EU as shipping2_2_1_, pricecolle0_2_.US_CURRENCY as us_curre1_8_1_, pricecolle0_2_.SHIPPING_COSTS_IN_US as shipping2_8_1_, case when pricecolle0_1_.EUROPE_PRICE_ID is not null then 1 when pricecolle0_2_.US_PRICE_ID is not null then 2 when pricecolle0_.PRICE_ID is not null then 0 end as clazz_1_ from PROJECT_HIBERNATE_Price pricecolle0_ left outer join PROJECT_HIBERNATE_EuropePrice pricecolle0_1_ on pricecolle0_.PRICE_ID=pricecolle0_1_.EUROPE_PRICE_ID left outer join PROJECT_HIBERNATE_USPrice pricecolle0_2_ on pricecolle0_.PRICE_ID=pricecolle0_2_.US_PRICE_ID where pricecolle0_.PRODUCT_ID=?
Hibernate: select product0_.PRODUCT_ID as product_1_5_0_, product0_.CUSTOMER_ID as customer8_5_0_, product0_.PRODUCT_CATEGORY_ID as product_9_5_0_, product0_.PRODUCT_DESCRIPTION as product_2_5_0_, product0_.PRODUCT_NAME as product_3_5_0_, product0_.PRODUCT_EXPIRES_AT_ as product_4_5_0_, product0_.PURCHASED_AT_ as purchase5_5_0_, product0_.PRODUCT_QUANTITY as product_6_5_0_, product0_.PRODUCT_STATUS as product_7_5_0_, product0_.PROMOTION_ID as promoti10_5_0_ from PROJECT_HIBERNATE_Product product0_ where product0_.PRODUCT_ID=?
Hibernate: select category0_.CATEGORY_ID as category1_0_, category0_.CATEGORY_DESCRIPTION as category2_0_, category0_.CATEGORY_NAME as category3_0_, category0_.PROMOTION_ID as promotio4_0_ from PROJECT_HIBERNATE_Category category0_ where category0_.CATEGORY_NAME=?
Hibernate: select productcol0_.PRODUCT_CATEGORY_ID as product_9_5_0_, productcol0_.PRODUCT_ID as product_1_5_0_, productcol0_.PRODUCT_ID as product_1_5_1_, productcol0_.CUSTOMER_ID as customer8_5_1_, productcol0_.PRODUCT_CATEGORY_ID as product_9_5_1_, productcol0_.PRODUCT_DESCRIPTION as product_2_5_1_, productcol0_.PRODUCT_NAME as product_3_5_1_, productcol0_.PRODUCT_EXPIRES_AT_ as product_4_5_1_, productcol0_.PURCHASED_AT_ as purchase5_5_1_, productcol0_.PRODUCT_QUANTITY as product_6_5_1_, productcol0_.PRODUCT_STATUS as product_7_5_1_, productcol0_.PROMOTION_ID as promoti10_5_1_ from PROJECT_HIBERNATE_Product productcol0_ where productcol0_.PRODUCT_CATEGORY_ID=?

soo in Product DB Table i have a Column: Product_Category_ID - referrencing foreign key to Category Table, witch is null even after i tried to link these together

But with CascadeType.ALL it works, strange



Solution 1:[1]

You can try to change your DNS to google like this:

https://www.youtube.com/watch?v=9BD264pKWzc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luiz Eduardo Araujo