Use select box to update foreign key relationship in table JPAContainer

HI,

I have an Entity Foo that has a oneToMany relationship to a category(every Foo belongs to a certain category).
I have used the JPAContainer to show all Foo entities in a table. Each row shows the Foo members (name, amount etc). The update is buffered, when I change a value this will be updated to the database when I hit the ‘save’ button.
So far this seems to work.
Now I want to add a column to the table containing a select box that I can use to assign a category to a Foo.
Selecting the category and hitting save should result in a foreign key(category_id) added to the table Foo pointing to the selected category in the category table.

I have tried a couple of things to accomplish this:

  • created the category select with the JPAContainer/provider. Changing values didn’t update to the database.
  • implemented a changeListener on the category select box. Looks like JPAContainer is already listening and select changelistener and JPAContainer are trying to recursively update the category.
  • added category.id as nestedProperty. changing the value in the table did not update the database.
  • Add the category as String instead of storing category_id as foreign key in Foo table. That worked, but is nog what I want.

Hope I made the problem clear. My questions:

  • Should JPAContainer update a nestedProperty, if changed, in the database automaticaly?
  • Can you point me in the right direction which approach to take in order to solve this problem.

Cheers.

Hi,

I assume that you are using the new JPAContainer 2.0.0-RC1.

Do I understand you correctly that the Table is in editable mode? In that case, you should be able to just set the field factory to the one provided in the JPAContainer package (com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory) and everything ought to work.

If your Table is not editable, you will need to create the select backed by another JPAContainer, containing categories, and the provided SingleSelectTranslator. E.g.

categorySelect.setContainerDataSource(categoryContainer);
categorySelect.setPropertyDataSource(new SingleSelectTranslator(categorySelect));

The reason for needing the translator is that each Foo-item is expecting an instance of the category entity, while JPAContainer uses IDs to identify items, not instances of the entities. The translator will make sure that IDs are converted to entities and vice versa as needed. Or you could probably use the FieldFactory to generate the select for you by calling createField with suitable parameters.

Using the approach above, there is no need to add a nested category.id property. And to answer your question, if you add a nested category.id property and update that, you will not actually be changing to which category the instance of Foo belongs, but you will be changing the ID of the category (the category will stay the same, but its ID will change). Well that came out pretty unclearly, but I hope you understood.

HTH,
/Jonatan

Hi Jonatan,

Thanks for the quick reply. This looks very usefull. Will have a look when I’m at home.

Regards…