tags:

views:

1333

answers:

1

What's the best way to implement an IDataProvider and a LoadableDetachable in Wicket for an indexed list? Suppose I have a Customer who has a list of Adresses.

class Customer { List adresses; }

Now I want to implement a data provider/ldm for the adresses of a customer. I suppose the usual way is an IDataProvider as an inner class which refers to the customer model of the component, like:

class AdressDataProvider implements IDataProvider {

public Iterator iterator() {
    Customer c = (Customer)Component.this.getModel(); // somehow get the customer model
    return c.getAdresses().iterator();
}

public IModel model(Object o) {
    Adress a = (Adress) o;
    // Return an LDM which loads the adress by id.
    return new AdressLoadableDetachableModel(a.getId());
}

}

Question: How would I implement this, when the adress does not have an ID (e.g. it's a Hibernate Embeddable/CollectionOfElements) but can only be identified by its index in the customer.adresses list? How do I keep reference to the owning entity and the index?

In fact, I know a solution, but I wonder if there's a common pattern to do this.

A: 

What is your proposed solution? Your question doesn't seem quite clear to me. Are the addresses loaded lazily by hibernate? I can't really see what your problem is with the above code. If your addresses get loaded in by Hibernate on the c.getAdresses().iterator(); call, then you have the addresses and what's the problem? Is the customer.adresses actually a list of address objects, or just id's? You can always record the owning entity and it's index inside your AdressLoadableDetachableModel i.e. AdressLoadableDetachableModel(a.getId(), (Customer)Component.this.getModel()) Can you help clarify?

Antony Stubbs