Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
SQLContainer Reference ForeignKey
I just have started trying out the SQLContainer. All worked fine and I was able to write into my SQL-db with
But I don't understand how to handle a foreignKey-relation between two tables/containers. Lets say there is a book table that has an "author_id" referencing to a author table. Then I can set the values like following:
Object id = bookContainer.addItem(); bookContainer.getContainerProperty(id, "title").setValue(book.getTitle()); bookContainer.getContainerProperty(id, "price").setValue(book.getPrice()); Object id_a = authorContainer.addItem(); authorContainer.getContainerProperty(id_a, "first_name").setValue(book.getAuthor().getFirstName); authorContainer.getContainerProperty(id_a, "last_name").setValue(book.getAuthor().getLastName);
The id_author is primary key in the author-table and set via AutoIncrement in MySQL. So how do I tell the bookContainer to fill author with the id that will be generated by MySQL for the new author?
I tried the following, but it does not work:
authorContainer.setReferencedItem("id_author", "author", bookContainer); bookContainer.addReference(authorContainer, "author", "id_author"); bookContainer.getContainerProperty(id, "author").setValue(bookContainer.getReferencedItem("id_author", authorContainer));
There is the error-message: java.lang.IllegalArgumentException: Reference to the given SQLContainer not defined.