Hi -

I was attempting a "too clever" scheme to defer adding to a collection that has not been lazy loaded yet. The code in the isPropertyLoaded() method below was posted by an expert on this list a while ago. It works - it returns true if the field has been loaded.

But the problem is that these deferred "members" are NOT included in the collection later when the collection is loaded - unless you do a flush first. In other words - new persistent members created in the current transaction are NOT included in the collection if the collection is loaded later (the members ARE in the database and show up in the NEXT transaction).

Probably I should give up on this approach - but just in case - is there another step that would make this work?

- Paul

@Entity
@Table (name="lazy_list_member")
public class LazyListMember
   implements java.io.Serializable
{
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Id private long id;
   @Version private long version;

   @ManyToOne (optional=false,fetch=FetchType.LAZY,
               cascade=CascadeType.ALL)
   private LazyListHolder holder;

   protected LazyListMember() {}
   public LazyListMember(LazyListHolder holder)
   {
       this.holder = holder;
       if (isPropertyLoaded(holder, "members"))
           holder.getMembers().add(this);
   }

   static boolean isPropertyLoaded(Object entity, String property)
   {
       PersistenceCapable pc = (PersistenceCapable)entity;
       StateManagerImpl sm = (StateManagerImpl)pc.pcGetStateManager();
       ClassMetaData metaData = sm.getMetaData();
       FieldMetaData fmd = metaData.getField(property);
       boolean isLoaded = sm.getLoaded().get(fmd.getIndex());
       System.err.println("---- isPropertyLoaded("+property+")="+isLoaded);
       return isLoaded;
   }
}


Reply via email to