You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by José Luis Cetina <ma...@gmail.com> on 2013/10/22 16:27:01 UTC

Immutable list in entity

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

Re: Immutable list in entity

Posted by Rick Curtis <cu...@gmail.com>.
Can you post the stacktrace of the exception you get?

Thanks,
Rick


On Sat, Nov 9, 2013 at 10:27 AM, José Luis Cetina <ma...@gmail.com>wrote:

> Rick in the link that you provide they report the same behavior,  is this a
> bug? Why my list attributes are not mutable after detached? Do you have any
> idea? The only way that i could still workong with openjpa is cloning my
> entire object when i want to modify any list attribute.
>  El 22/10/2013 12:04, "Mark Struberg" <st...@yahoo.de> escribió:
>
> >
> >
> > 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 <cu...@gmail.com>
> > >To: users <us...@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 <
> maxtorzito@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 <al...@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 <
> > maxtorzito@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*
> > >
> > >
> >
>



-- 
*Rick Curtis*

Re: Immutable list in entity

Posted by José Luis Cetina <ma...@gmail.com>.
Rick in the link that you provide they report the same behavior,  is this a
bug? Why my list attributes are not mutable after detached? Do you have any
idea? The only way that i could still workong with openjpa is cloning my
entire object when i want to modify any list attribute.
 El 22/10/2013 12:04, "Mark Struberg" <st...@yahoo.de> escribió:

>
>
> 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 <cu...@gmail.com>
> >To: users <us...@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 <maxtorzito@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 <al...@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 <
> maxtorzito@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*
> >
> >
>

Re: Immutable list in entity

Posted by Mark Struberg <st...@yahoo.de>.

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 <cu...@gmail.com>
>To: users <us...@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 <ma...@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 <al...@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 <maxtorzito@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*
>
>

Re: Immutable list in entity

Posted by Rick Curtis <cu...@gmail.com>.
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 <ma...@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 <al...@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 <maxtorzito@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*

Re: Immutable list in entity

Posted by José Luis Cetina <ma...@gmail.com>.
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 <al...@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 <maxtorzito@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*
-------------------------------------------------------------------

Re: Immutable list in entity

Posted by Albert Lee <al...@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 <ma...@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.