Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to set JPA entity's foreign key value with Combobox?
is there any example which explains how to use combobox, in order to set foreign key field value?
For example, imagine typical Department/Employees relationship, where on the Employee entity exists deptId field (BigDecimal), representing foreign key to the Department. Now, in order to show Department choices,on the Employee form, I am using Combobox filled with BeanItemContainer built on Department entitties. But, how to ensure that Employee's.deptId field are filled with appropriate Department.Id, when user choose something from the combobox?
Should I use Converter for the Combobox, and if so, how?
Thank in advance,...
it seems that the forum is not particularly active....
Do you have a proper relation in your JPA model or manually working with identifieers? The combobox's value should be the same as the edited type, then no converters are needed.
Check this example app:
It don't actually have an example of ManyToOne relation, but there is ManyToMany. The principle is the same in both, just that in ManyToOne you can use ComboBox. I actually suggest to use TypedSelect from Viritin instead of combobox, that contains bettter typed API (and no need to wrap your options into BeanItemContainer) that make things much more easier to understand.
Actually, in the Employee entity, I have
@ManyToOne @JoinColumn(name="DEPT_ID", insertable=false, updatable=false) private Department master_dept;
@Column(name = "DEPT_ID") private BigDecimal deptId;
But, as you can see, I am forced to use
This is because, without that, I am stuck with
Multiple writable mappings exist for the field [Employee.DEPT_ID]. Only one may be defined as writable, all others must be specified read-only.
So, if I understand you correctly, what you suggest, is to use cobombox for the @ManyToOne master_dept field, but I cannot do that. I ended up with Converter between Object (holding by Combobox) and BigDecimal (deptId)
Do you have any other suggestion?