Referenced Items

Hi Guys,

today i got a Question about the Referenceing of SQLConttainers/Items.

When i try to set a Referenced Item, that hasent been commited to the DB yet (and so only has a Temporary Row ID, no ItemId (Primary Key)) there always is a Illegal value type for ColumnProperty Exception.
Heres the code:

orders.setReferencedItem(newOrderId, newShippingId, shippingAdressData);

newOrderId is the TemporaryRowId of a new Item in orders(SQLContainer)
newShippingId is an IitemId (Primary Key) of an existing Row in the DB

So i guess he is complaining about the newOrderId, that not is a Real ItemId but only a TemporayRowId.

The Problem is, that the ForeignKey, that will be set by newShippingId (in orders Table) isnt nullable, so i cannot commit an empty new Item to the DB and then fetch back the correct Primary Key of the new Row in orders.

How would you guys solve that problem? Maybe there is an obvious solution, but i dont see it :frowning:

Tanks, Jan

ok, I have spent alot time in debug mode now.
I found some lines in the com.vaadin.data.util.sqlcontainer.SQLContainer.class that dont make sense in my opinion.

Here is the code (lines 1758 - 1779):

public boolean setReferencedItem(Object itemId, Object refdItemId,
            SQLContainer refdCont) {
        if (refdCont == null) {
            throw new IllegalArgumentException(
                    "Referenced SQLContainer can not be null.");
        }
        Reference r = references.get(refdCont);
        if (r == null) {
            throw new IllegalArgumentException(
                    "Reference to the given SQLContainer not defined.");
        }
        try {
            getContainerProperty(itemId, r.getReferencingColumn()).setValue(
                    refdCont.getContainerProperty(refdItemId,
                            r.getReferencedColumn()));
            return true;
        } catch (Exception e) {
            getLogger()
                    .log(Level.WARNING, "Setting referenced item failed.", e);
            return false;
        }
    }

lets focus on this part:

getContainerProperty(itemId, r.getReferencingColumn()).setValue(
                    refdCont.getContainerProperty(refdItemId,
                            r.getReferencedColumn()));

in my opinion, there is a missing .getValue

getContainerProperty(itemId, r.getReferencingColumn()).setValue(
                    refdCont.getContainerProperty(refdItemId,
                            r.getReferencedColumn()).[b]
getValue()
[/b]);

becuse, in the original code it trys to set a Property as the Value of the Property of the referencing Item. That does not make sense. (… i think)
so a simple .getValue should set the
value
of the referenced Property as the
value
of the referencing Property.
Can anybody follow me? Am I Wrong?

Fully agree, I’m facing the same problem setReferencedItem() simply doesn’t work due to this obvious bug. Would be nice to have it fixed it in the next Vaadin update.

the workaround is simple but not too aesthetic:
container1.getItem(itemId1).getItemProperty(“referencing_property”).setvalue(
container2.getItem(itemId2).getItemProperty(“referenced_property”).getvalue());