Lazy stuff not usable at all on the client side ?
elkner Apr 27, 2005 1:10 PMActually I've a big problem, to understand, how a client should/can use the lazy fields of an entity.
Up to now, my conclusion is, that lazy stuff can't be used on the client side at all, since (at least with jboss) we have a complete inconsistent behavior. E.g.:
private Collection<A_Product> products; @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) @AssociationTable(table = @Table(name="products_to_categories"), joinColumns = { @JoinColumn(name="categories_id", referencedColumnName="categories_id") }, inverseJoinColumns = { @JoinColumn(name="products_id", referencedColumnName="products_id") }) public Collection<A_Product> getProducts() { return products; }
But even if I try to access the products via a Dao in the following way, I still get LazyInitializationException: failed to lazily initialize a collection..., when I try to acces the collection on the client side.
public Collection<A_Product> getProducts(A_Category category) { if (category == null) { return new ArrayList<A_Product>(0); } manager.refresh(category); return category.getProducts(); }
So what I have, is IMHO NOT a POJO but something beetween a POJO and RMI object, since the "pseudo" POJO still tries to invoke methods on the server side, which wrt. lazy objects or at least collections miserably fails., because it returns not the required object but a kind of "remote" reference to a not-working "thing".
So, AFAIU at least at the moment and on the client side, EJB3 is not, what it ought to be.
With tradional RMI one would probably simply call something like rmiObject.getProducts() and than one would get the serialized collection.
Why is this different handled in EJB3 ? Do I need to write a DAO wrapper for every lazy object/collection of an entity (like return manager.getProducts().toArray[new A_Product[0]) ?
But using the latter approach, one gets a "could not initialize proxy - no Session" when one tries to access a property of the product, even if it is not lazy (just e.g. a String) ...
Of course, one could probably use EAGER, but if you have tried that with a little bit more complex database (i.e. more than 1 or 2 [in]direct references), one quickly sees, that this is not an option, since this is incredible slow and unneccessarily loads stuff into memory, which is not needed :(((
So, has anybody a kind of howto or "best practice" tip, how lazy fields of an entity can be used on the client side? Or is the implementation in jboss simply bogus ?