Lists we get from query.getResultList() are of course immutable. 

But if I have an entity which has a @ElementCollection or a @OneToMany 
List<OtherEntity> then those imo should be mutable. Regardless whether in 
attached or detached state..

It is obvious for @ElementCollections but also for e.g. @OneToMany with 
CascadeType.ALL.

Otherwise entityManager.merge() would not make much sense imo...

I was always under the impression that this works perfectly fine, even in 
detached state. We do not exchange the lists in those entities when we perform 
a detachment afaik. We only change the StateManager to a DetachedStateManager, 
but thats's it basically.

LieGrue,
strub




>________________________________
> From: Rick Curtis <curti...@gmail.com>
>To: users <users@openjpa.apache.org> 
>Sent: Tuesday, 22 October 2013, 18:30
>Subject: Re: Immutable list in entity
> 
>
>I don't have a solid answer, but have found a number of posts throughout
>the years on markmail[1]. I'm just heading out to lunch, but I'll read
>through those sometime later today.
>
>[1] http://openjpa.markmail.org/search/?q=%22Result+lists+are+read+only%22
>
>
>
>On Tue, Oct 22, 2013 at 11:05 AM, José Luis Cetina <maxtorz...@gmail.com>wrote:
>
>> Sorry about String immutable, let my explain me.
>>
>>  (The entities is just an example for demostrate this)
>>
>> @Entity
>> private MyClass implements Serializable{
>>
>> private String name="old value";
>> @ManyToOne
>> private List<Product> products;
>> // getters and setters..
>> }
>>
>> Example 1
>> 1. Get MyClass entity in a managedbean from and ejb (ejb use a query and
>> the products attribute is null because is LAZY LOADING).
>> 2. inside of the managedbean try to set a new value for name
>> myClass.setName("new value); //the value is refreshed from "old value" to
>> the new value "new value"
>> 3. inside of the managedbean try to set a new list of products like:
>>     List<Product> myNewList = new ArrayList<Product>(new Product());
>>     //then try to set this new list to the entity, remeber that we are in
>> the managedbean and the entity is detached
>>     myClass.setProducts(myNewList); // here i set the new list but no
>> matter what list of product i set to my entity the list of product remains
>> null, like if the list is like readonly
>>     myClass.getProducts().size();//i get a nullpointer here
>>
>> Example 2
>> 1. Get MyClass entity in a managedbean from and ejb (ejb use a query and
>> the products attribute contains N elements (IS NOT EMPTY OR NULL) because
>> is EAGER LOADING).
>> 2. inside of the managedbean try to set a new value for name
>> myClass.setName("new value);  //the value is refreshed from "old value" to
>> the new value "new value"
>> 3. inside of the managedbean try to clear the list of the entity you get an
>> exception
>>     myClass.getProducts().clear(); // here i get an exception "Result lists
>> are read only"
>>
>>
>> I cannot find anywhere in the specification like "results lists have to be
>> readonly and other values not".
>>
>> This is why i asked "the list values have to be immutables in an entity
>> even when the entity is detached?"
>>
>>
>> Then i ask again, why list are read only and any other value not?
>>
>>
>>
>>
>> 2013/10/22 Albert Lee <allee8...@gmail.com>
>>
>> > String type is immutable, meaning if you are holding on a reference to
>> it,
>> > it will never be changed even it is "modified" since a new
>> object/reference
>> > will be created.
>> >
>> > List type is not immutable, regardless of if it is used in an JPA Entity,
>> > either managed or detached.
>> >
>> > Not sure exactly what you are looking for but an example may be helpful
>> > here.
>> >
>> >
>> >
>> > On Tue, Oct 22, 2013 at 9:27 AM, José Luis Cetina <maxtorz...@gmail.com
>> > >wrote:
>> >
>> > > Hi, i want to know if the default openjpa behavior about list
>> attributes
>> > in
>> > > an entity
>> > >
>> > > When i retrieve and entity (that has list values) and detached it from
>> em
>> > > and then return (from ejb) to my web tier (jsf app), the list values
>> are
>> > > immutable but other object are mutables like strings, I cannot find in
>> > the
>> > > specification if this is the expected behavior, the list values have to
>> > be
>> > > immutables in an entity even when the entity is detached?
>> > >
>> > > Thanks
>> > >
>> >
>> >
>> >
>> > --
>> > Albert Lee.
>> >
>>
>>
>>
>> --
>> -------------------------------------------------------------------
>> *SCJA. José Luis Cetina*
>> -------------------------------------------------------------------
>>
>
>
>
>-- 
>*Rick Curtis*
>
>

Reply via email to