0 Replies Latest reply on Mar 6, 2006 12:04 AM by mazz

    refresh versus find

    mazz

      I'm trying to figure out when refresh is more appropriate than find and vice versa.

      Here's my concern - I want my client code to work with the entity POJOs as much as possible. I don't want to have to pass around primary key classes around if I can help it.

      When my client calls into a session bean, and the client needs to pass an entity to the session bean, I'd like it to pass an entity POJO with just the primary key set; all other attributes are not set (null).

      Now my session bean can conceivably call EM.refresh(POJO) to find/load the entity. This is different than if the client just passed the PK; the session bean would have to EM.find(POJO.class, PK).

      Is this an appropriate thing to do? Using refresh, I'd rely on my LAZY/EAGER loading to load the entity - which I assume is the same as if I did a find() (so I don't think there is any performance difference between the two methods).

      Example:

      client code does "new Person(1)" to create a Person entity POJO with PK of 1. It can then pass the Person object to the session bean where EM.refresh(person) is then called.

      The client could do a "new Integer(1)" and pass that integer to the session bean which then calls EM.find(Person.class, integer).

      I don't see any performance issues between the two - they should operate the exact same way (they lazy load or eager load as appropriate). But I think it makes the code a little more readable and maintainable if the client always uses the entity POJO rather than a generic integer PK objects (passing a Person is more informative as to what I'm passing as opposed to passing a generic Integer - I know I'm dealing with a Person in former case; in the latter, I'd have to figure out another way what I'm passing in - is it a PK for a Person or a PK for a Manager or a PK for an Employee (assuming all have Integer as their PK type).

      I'm just looking for some opinions. Also, am I reading correctly the behavior of refresh() or am I missing something that I haven't considered?