Thanks for the feedback.
Judes suggestion looks promising for ManyToMany case but could
complicate my forward mapping.
Pinaki - Your suggestion sort of works ... because it creates a new
unmodifiableList every time getMyPcList() is called -- there is a constructor
invoked inside Collections.unmodifiableList(myPcList). It doesn't work
perfectly though because a client can still wind up holding on to a stale
unmodifiableList.
I am using both the unmodifiableList to return to clients and the original List
to add objects to the List. This is tricky when myPcList starts out null and
later gets replaced by a proxy which makes the wrapped unmodifiableList stale
(my understanding of what happens). I'm not sure what combination creates this
stale case - I have a test that results in the size() of the stale unmodifiable
List being smaller than the size() of the original List.
As I said this is all in on Transaction.
- Paul
On Wed, Apr 8, 2009 at 6:02 AM, Pinaki Poddar <[email protected]> wrote:
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);
}
On 4/8/2009 6:41 AM, Judes Tumuhairwe wrote:
I think making both join-columns not insertable and not updatable should
work.On the promotion (ownerSide) object
@JoinTable(name = "promotion_pc", schema="public",
joinColumns = {...@joincolumn(name = "promotion_id",
referencedColumnName = "id", insertable=false, updatable=false)},
inverseJoinColumns = {...@joincolumn(name = "pc_id",
referencedColumnName = "id", insertable=false, updatable=false)}
)
@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private List<MyObject> myObject;
public List<MarketingAsset> getMarketingAssets()
{
if (marketingAssets == null)
marketingAssets = new ArrayList<MarketingAsset>();
return Collections.unmodifiableList(marketingAssets);
}
On Wed, Apr 8, 2009 at 6:02 AM, Pinaki Poddar <[email protected]> wrote:
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.