Using Springboot 2.4.0, I am trying to develop a simple module to manage a tipical relationship between the entities Product and Order (ManyToMany relationship).
In Order, I have this products field:
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name="products_orders",
joinColumns=@JoinColumn(name="order_id"),
inverseJoinColumns=@JoinColumn(name="product_id")
)
private List<Product> products;
Since I don't need it, for now I am omitting an orders field in the Product entity.
Previous to this relationship, I have a CRUD for Product entity up and properly running, so I can insert products through a Product Repository:
public interface ProductRepository extends JpaRepository<Product, Integer>
I need to place an order with a number of products within it. I have this code:
List<Products> myListOfProducts = new ArrayList<>();
myListOfProducts.add(previouslyExistingProduct);
myListOfProducts.add(nonExistingProduct);
Order myorder = new Order(myListOfProducts, "mail", LocalDateTime.now());
myorder.save();
The question is: is it possible to get with the previous code that the new order gets saved as well as the products linked to it (saving both the existing and non existing products)?
This is the Order Repository:
public interface OrderRepository extends JpaRepository<Order, Integer>
This is what I got so far:
- If I change CascadeType to
CascadeType.MERGE, I get to link only existing products to the new order, but for new Products, I get this error:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gallelloit.productcrud.model.Product; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gallelloit.productcrud.model.Product
- Any other value I put to CascadeType (including
CascadeType.ALL), I get the opposite: new products are saved and linked to the new order, but for existing products, I get this error:
detached entity passed to persist: com.gallelloit.productcrud.model.Product; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.gallelloit.productcrud.model.Product
I know that I can do it "manually" getting each of the products, and saving them before the order.save(), but I was wondering if Spring Data did it for you.
Any idea?