openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <ppod...@apache.org>
Subject Re: Pattern for unmodifiable Collections
Date Wed, 08 Apr 2009 10:02:48 GMT

Hi,

> Is there a way to prohibit direct updates to a perisistent Collection? 

What exactly happens if you wrap the list as unmodifiable as follows?
    @ManyToMany (mappedBy="ownerSide", fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
    private List<MyPcObject> myPcList;

    List<Promotion> getMyPcList()  {
        if (myPcList == null)
            myPcList = new ArrayList<MyPcObject>();
        return Collections.unmodifiableList(myPcList);
    }

OpenJPA proxies the collections and it is possible to tune these proxies including tracking
changes them [1]. But that will involve more work from the application.

[1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_pc_scos_proxy_custom


I am trying to provide JPA persistent objects in a framework.  If I 
return a ManyToMany Collection the user has to understand what side is 
the mappedBy owner and be careful to update both sides.  There are other 
reasons for wanting to restrict updates to OneToMany relations.  Is 
there a way to prohibit direct updates to a perisistent Collection? 

    @ManyToMany (mappedBy="ownerSide", fetch=FetchType.LAZY, 
cascade=CascadeType.PERSIST)
    private List<MyPcObject> myPcList;

    List<Promotion> getMyPcList()
    {
        if (myPcList == null)
            myPcList = new ArrayList<MyPcObject>();
        return myPcList;
    }

I tried wrapping the List in a Collections.unmodifiableList(list) but it 
looks like OpenJPA can replace the List with another object so the List 
I wrapped becomes stale. (Does that make sense by the way - should I 
expect that to happen?)

Any suggestions would be appreciated!

- Paul






-----
Pinaki Poddar                      http://ppoddar.blogspot.com/
                                      
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
-- 
View this message in context: http://n2.nabble.com/Pattern-for-unmodifiable-Collections-tp2600278p2604302.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message