JPA + Form + @ManyToto not saved to db

Hello,
I am using Vaadin 23.3.3 with Springboot.

Orders Bean

`
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long orderId;

private String customerName;
private String firmName;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = OrderItemDetail.class)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private List itemDetails;

private Double orderTotalAmount;
`

OrderItemDetails

`@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long itemDetailsId;

private String itemName;
private Double itemQuantity;
private Double itemCost;
private Double itemTotalCost;`

I have binder on Order in Form
Binder<Order> binder = new BeanValidationBinder<>(Order.class);

When SaveEvent is triggered, the order is save but the OrderItemDetails is not being saved,

I have looked up on internet but unable to find a solution

Please take a look at https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

Thank you, will check it out.

wha about the binder? Does the Form fields auto bind to the orderItemdetails?

It depends if you provided proper fields for it. By default it can’t because there is no field that can be associated with this custom class.

I have fields in the form and trying to bind the fields as below

binder.forField(itemName) .bind(OrderItemDetail::getItemName, OrderItemDetail::setItemName);

but gettin error Non-static method cannot be referenced from a static context

For the Order, i use auto binder
binder.bindInstanceFields(this);

I don’t think you can use bindInstanceFields or did you create a custom field with List as generic param?

not a generic param.
I modified the code to use binder.ForField().bind()

so now my form has 2 binders

Binder<Order> binder = new BeanValidationBinder<>(Order.class);
Binder<OrderItemDetail> itemBinder = new BeanValidationBinder<>(OrderItemDetail.class);

but when i try to writeBean, I get error →
java.lang.NullPointerException: bean cannot be null

itemBinder.writeBean(orderItemDetail);

Is it possible to have 2 binders in a single form?

Sure

@amazing-wolf Check out ElementCollectionField helper in Viritin add-on. Might be exactly the field for your Orders.itemDetails property.

And a pro tip: use setBean instead of readBean/writeBean when using bean validation. The “buffered mode” doesn’t help at all as your detached entities are buffering things already.