openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Franck Wolff <>
Subject Re: Lazy initialisation questions
Date Tue, 16 Dec 2008 17:17:55 GMT

Thanks for your answer (sse more below)!

> Hi,
>   Hopefully, you are familiar with configuration options that may be
> relevant in similar scenarios. For example, DetachState flags such as
> "AccessUnloaded". 
>> *1. How do I figure out what fields of a detached object are
>> uninitialized?*
> The bit index of getLoaded() are same as the index used in ClassMetaData. So
> the following *should* work (but I am writing this on-line without testing,
> please verify)
> PersistenceCapable pc = (PersistenceCapable)myEntity;
> OpenJPAStateManager sm = (OpenJPAStateManager)pc.pcGetStateManager();
> BitSet loaded = sm.getLoaded();
> for (int i = 0; i < loaded.size(); i++) {
>  FieldMetaData fmd = sm.getMetaData().getField(i);
>  System.err.println(fmd.getFullName() + (loaded.get(i) ? ": loaded" ? ": not
> loaded");
> }
Ok. This seems to be exactly what I was looking for.
>> 2. How do I get the id of a uninitialized single-valued association
>> object?*
>> I have seen this in some code sample (but, again, I need to know the 
>> index of the Java field):
>> Object proxy =
>> ((OpenJPAStateManager)pc.pcGetStateManager()).getIntermediate(index);
>> Object id = ((OpenJPAId)proxy).getIdObject();
>> Is it reliable?
> Bit hacky, might work. But what is the usage? If the single-valued
> association object is not fetched during detach, the remote tier will not
> have access to it anyway. If it has been fetched during detach, then there
> will be more direct (and kosher) way to get its identifier's value. 
Ok, some more explanations (apply to question 3 as well):

Again, let's say I have a entity bean like this one:

public class Entity1 {

    private Integer id;

    @OneToOne(fetch=FetchType.LAZY, ...)
    private Entity2 other;

    @OneToMany(fetch=FetchType.LAZY, ...)
    private Set<Entity3> others;


If I get an instance of this bean from an entity manager, the "other" 
and "others" fields will be null in the detached object (even if there 
is a non null value for other and a non empty collection for others in 
the database). Suppose now that I create a brand new instance of Entity1 
and set its id to the id I got in the previous bean: other and others 
will be null, this object won't have any detached information and when I 
will merge this new bean with an entity manager, I must be sure that the 
other/others values in the database won't be overwritten (ie: the new 
bean should be treated as if it was a detached object with uninitialized 
other/others associations).

This is exactly the situtation in the Web layer at deserialization time 
(ActionScript3 -> Java): I don't have any *true* detached object, I just 
need to create one and recreate uninitialized properties.

I have actually made some tests with uninitialized collections and it 
seems that collection fields set to null are treated just as if they 
were uninitialized (the collection isn't overwritten in the database). 
However, I don't see how OpenJPA could distinguishe between a 
single-valued association set to null (ie: actually set to null in the 
database) and a lazy-loaded association (set to null in the bean but not 
in the database) without any meta informations (such as a "loaded" 
BitSet or whatever).

Hope this is clear ;-)

>> *3. How do I recreate an OpenJPA detached object?*
>   Tough. Thinking...
>   But why?

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message