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

Entity cant be refreshed with new list values

I sent this mail to openJPA mailing list but i dont know if this could be
something related to TomEE or just im doing something wrong.


I have a "strange" behavior, with list properties of any kind of entity.

When i try to set a non empty list to a retrieved entity that has a list
value with null (because is not fetched) the entity doesnt "reflect" the
new non-empty values that i set.

Steps
1. Retrieve any entity (ex. user) from database in an ejb with criterias.
2. The retrieved entity has a OneToMany attribute (roles) by default is
lazy thats why if i dont do a fetch (i dont) the list is null
3. Then try to fill the role list with a NON-EMPTY list
4. Here the list that i set has values but the user list has a null in the
roles list attribute even though i set it in the step 3


I dont know why this is happening, the only way i could fix this is cloning
(using SerializationUtils from Apache commons.lang3) the user entity (see
the steps) and with this work, but i dont know why.

1. Retrieve any entity (ex. user) from database in an ejb with criterias.
2. Clone retrieved entity and asig to new one User clonedUser =
SerializationUtils.clone(originalRetrivedUser);
3. Then try to fill the role list with a NON-EMPTY list to the clonedUser
4. The list of clonedUser has the correct values


Why i need to clone? why i cannot set a list to the entity if is not cloned?


Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version embedded)


Scenario:

***** ENTITIES *****
@Entity
public class User implements Serializable{

   @Id
   private int id;
   private String userName;
   private String password;
   @OneToMany(mappedBy = "user")
   private List<Role> roles;

   //getters and setters..

}


@Entity
public class Role implements Serializable{

   @Id
   private int id;
   private String roleName;
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User user;

  //getters and setters..
}


**** EJB CLASS ****
@Stateless
public class MyEJB{

    @PersistenceContext(unitName ="ANY_NAME")
    private EntityManager em;

  public User getUserWithRoles(){

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> cq = cb.createQuery(User.class);
        Root<User> root = cq.from(User.class);

        cq.where(cb.equal(root.get(User_.userName),"john"));

       User userJohn = em.createQuery(cq).getSingleResult();  // if i want
this work i have to do User userJohn =
SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]


       //i will create a list of role just for try to set any role the
userJohn
      List<Role> roleList = new ArrayList<Role>(2);
      roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
      roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2

     //setting the list of roles created to the user, as you can see the
list has 2 values but the value roles of userJohn always is set to null, my
setters and getters are correct
     userJohn.setRoles(roleList);

     return userJohn;
  }

}

*** MANAGED BEAN ****

@Named
public class MyBean implements Serializable{

 @EJB
 private MyEJB ejb;

 public void anyMethod(){
   User user = ejb.getUserWithRoles();
    user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i use
clone in the ejb method (see 1*) i can get the corrected values.

 }


}

I know i can get the values fetching but im trying to not do it

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
Jose',

On Sun, Oct 20, 2013 at 3:50 AM, José Luis Cetina <ma...@gmail.com>wrote:

> What about using a detached entity?? The detached entity will work like a
> DTO?
>

I am doing plenty of populating detached entities, and then populate
database from detached entities by methods (that I wrote) to reference
every getter method and use setter method of new managed entity...and then
persist via JPA.


>
> From Real World Java EE Patterns (Adam Biem) 2009
> Problem
> The origin problem statement was: “You want to transfer multiple data
> elements over a tier”
> (
>
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> ).
>

transfer multiple data elements over 'a' tier or 'multiple' tiers? i only
have one-tier (tomee + webapps + jdbc), and I think I am transferring data,
successfully, over a/one tier. :)

i just tried to click on that URL, and it takes me to oracle's home page. :(

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Thats why i still figthing with my entities.  I didnt know that undetached
entites are read only, but this happen with list values because with any
other values like string i could change it. Thats why i never have figure
it this limitation until rigth now that i need to modify a list.
El 20/10/2013 14:52, "Romain Manni-Bucau" <rm...@gmail.com> escribió:

> Unmanaged entities are almost read only
> Le 20 oct. 2013 21:48, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
> > Whats is not supported by openjpa?
> > El 20/10/2013 14:22, "Romain Manni-Bucau" <rm...@gmail.com>
> > escribió:
> >
> > > Openjpa clearly doesnt support today...and no Mark it can or not work
> by
> > > spec...
> > > Le 20 oct. 2013 18:04, "José Luis Cetina" <ma...@gmail.com> a
> > écrit :
> > >
> > > > Responses inline.
> > > >
> > > > El 20/10/2013 06:51, "Mark Struberg" <st...@yahoo.de> escribió:
> > > > >
> > > > > Romain, that's nowhere in the spec. Thus it must work. Really!
> > > >
> > > > If this is true, what im doing wrong? Or this is openjpa or tomee
> > issue?
> > > >
> > > > >
> > > > > The only thing which is specified to be immutable are lists
> returned
> > by
> > > > query.getResultList.
> > > > > That's the reason why you should not back a sortable h:dataTable
> by a
> > > > list you get from JPA directly.
> > > > > All other stuff is perfectly mutable.
> > > > >
> > > > > LieGrue,
> > > > > strub
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > >________________________________
> > > > > > From: Romain Manni-Bucau <rm...@gmail.com>
> > > > > >To: users@tomee.apache.org
> > > > > >Sent: Sunday, 20 October 2013, 10:00
> > > > > >Subject: Re: Entity cant be refreshed with new list values
> > > > > >
> > > > > >
> > > > > >Not really. An entity handles a state which can prevent it.
> Nothing
> > > > > >mandates it to work
> > > > > >
> > > > > >Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com>
> a
> > > > écrit
> > > > :
> > > > > >
> > > > > >> What about using a detached entity?? The detached entity will
> work
> > > > like a
> > > > > >> DTO?
> > > > > >>
> > > > > >> From Real World Java EE Patterns (Adam Biem) 2009
> > > > > >> Problem
> > > > > >> The origin problem statement was: “You want to transfer multiple
> > > data
> > > > > >> elements over a tier”
> > > > > >> (
> > > > > >>
> > > > > >>
> > > >
> > > >
> > >
> >
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> > > > > >> ).
> > > > > >> This particular problem was elegantly solved in Java EE 5 with
> > > > detachment
> > > > > >> of persistent entities. There is
> > > > > >> no need for the introduction of another class just for
> > transferring
> > > > the
> > > > > >> entities data. JPA entities can
> > > > > >> implement a java.io.Serializable interface and be transferred
> > > between
> > > > > >> tiers, even remote ones.
> > > > > >> CMP 2.x entities weren’t Serializable, the developer was forced
> to
> > > > copy
> > > > > >> their states to a remotely
> > > > > >> transferable structure—the Transfer Object.
> > > > > >>
> > > > > >>
> > > > > >>
> > > > > >> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
> > > > > >>
> > > > > >> > responses below...
> > > > > >> >
> > > > > >> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <
> > struberg@yahoo.de
> > > >
> > > > > >> wrote:
> > > > > >> >
> > > > > >> > > be careful with immediate=true. You get all sorts of nasty
> > side
> > > > > >> effects.
> > > > > >> > >
> > > > > >> > > see page 92 in
> > > > > >> > >
> > > > > >> >
> > > > > >>
> > > >
> > > >
> > >
> >
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> > > > > >> > >
> > > > > >> > >
> > > > > >> > I definitely agree and understand about immediate=true, and
> > guess
> > > > what, i
> > > > > >> > found it very useful to disable validation as instructed on
> page
> > > 92
> > > > of
> > > > > >> the
> > > > > >> > PDF file.
> > > > > >> >
> > > > > >> > clarification of my use/understanding:
> > > > > >> >
> > > > > >> > i am 'not' using immediate=true, when i user is to select a
> row
> > on
> > > > > >> > datatable, and then click commandbutton/link/menuitem, which
> > does
> > > a
> > > > POST
> > > > > >> of
> > > > > >> > the selected row on the datatable, and bean uses the selected
> > row
> > > to
> > > > > >> > prepare the UI for the next view that is 'rendered' via
> > > > > >> > ui:include=#{bean.page}. see below and keep reading, please.
> > > > > >> >
> > > > > >> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> > > > > >> >
> > > > > >> >
> actionListener="#{pf_pointOfContactController.prepareCreate()}"
> > > > > >> >
> > > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > > >> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> > > > > >> >
> > > > > >> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> > > > > >> >
> > > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > > >> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> > > > > >> >
> > > > > >> > actionListener="#{pf_pointOfContactController.prepareView()}"
> > > > > >> >
> > > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > > >> >     <p:separator/>
> > > > > >> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon
> > > > ui-icon-newwin"
> > > > > >> >
> > > > > >> >
> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> > > > > >> >
> > > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > > >> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> > > > > >> >
> > > > > >> >
> > > > > >>
> > > >
> > >
> >
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> > > > > >> >
> > > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > > >> >
> > > > > >> > but, for a readonly page that has commandbutton/links to
> render
> > a
> > > > new
> > > > > >> view,
> > > > > >> > based on the current @Entity that is held in the JSF
> > > controller/bean
> > > > > >> class,
> > > > > >> > i use immediate=true without issue and I think it fits/meets
> the
> > > > > >> > occasion/requirement, because there is no need to do
> validation
> > > > phase or
> > > > > >> > update model values. see below. :)
> > > > > >> >
> > > > > >> >     <p:commandButton value="Browse" icon="ui-icon-search"
> > > > > >> immediate="true"
> > > > > >> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > > >> >
> > > > > >> >
>  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> > > > > >> >     <p:commandButton value="Delete" icon="ui-icon
> ui-icon-trash"
> > > > > >> > immediate="true"
> > > > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > > >> >
> > > > > >> >
> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> > > > > >> >     <p:commandButton value="Edit" icon="ui-icon
> ui-icon-pencil"
> > > > > >> > immediate="true"
> > > > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > > >> >
> > > > > >> >
>  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> > > > > >> >
> > > > > >>
> > > > > >>
> > > > > >>
> > > > > >> --
> > > > > >>
> > -------------------------------------------------------------------
> > > > > >> *SCJA. José Luis Cetina*
> > > > > >>
> > -------------------------------------------------------------------
> > > > > >>
> > > > > >
> > > > > >
> > > >
> > >
> >
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Unmanaged entities are almost read only
Le 20 oct. 2013 21:48, "José Luis Cetina" <ma...@gmail.com> a écrit :

> Whats is not supported by openjpa?
> El 20/10/2013 14:22, "Romain Manni-Bucau" <rm...@gmail.com>
> escribió:
>
> > Openjpa clearly doesnt support today...and no Mark it can or not work by
> > spec...
> > Le 20 oct. 2013 18:04, "José Luis Cetina" <ma...@gmail.com> a
> écrit :
> >
> > > Responses inline.
> > >
> > > El 20/10/2013 06:51, "Mark Struberg" <st...@yahoo.de> escribió:
> > > >
> > > > Romain, that's nowhere in the spec. Thus it must work. Really!
> > >
> > > If this is true, what im doing wrong? Or this is openjpa or tomee
> issue?
> > >
> > > >
> > > > The only thing which is specified to be immutable are lists returned
> by
> > > query.getResultList.
> > > > That's the reason why you should not back a sortable h:dataTable by a
> > > list you get from JPA directly.
> > > > All other stuff is perfectly mutable.
> > > >
> > > > LieGrue,
> > > > strub
> > > >
> > > >
> > > >
> > > >
> > > > >________________________________
> > > > > From: Romain Manni-Bucau <rm...@gmail.com>
> > > > >To: users@tomee.apache.org
> > > > >Sent: Sunday, 20 October 2013, 10:00
> > > > >Subject: Re: Entity cant be refreshed with new list values
> > > > >
> > > > >
> > > > >Not really. An entity handles a state which can prevent it. Nothing
> > > > >mandates it to work
> > > > >
> > > > >Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a
> > > écrit
> > > :
> > > > >
> > > > >> What about using a detached entity?? The detached entity will work
> > > like a
> > > > >> DTO?
> > > > >>
> > > > >> From Real World Java EE Patterns (Adam Biem) 2009
> > > > >> Problem
> > > > >> The origin problem statement was: “You want to transfer multiple
> > data
> > > > >> elements over a tier”
> > > > >> (
> > > > >>
> > > > >>
> > >
> > >
> >
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> > > > >> ).
> > > > >> This particular problem was elegantly solved in Java EE 5 with
> > > detachment
> > > > >> of persistent entities. There is
> > > > >> no need for the introduction of another class just for
> transferring
> > > the
> > > > >> entities data. JPA entities can
> > > > >> implement a java.io.Serializable interface and be transferred
> > between
> > > > >> tiers, even remote ones.
> > > > >> CMP 2.x entities weren’t Serializable, the developer was forced to
> > > copy
> > > > >> their states to a remotely
> > > > >> transferable structure—the Transfer Object.
> > > > >>
> > > > >>
> > > > >>
> > > > >> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
> > > > >>
> > > > >> > responses below...
> > > > >> >
> > > > >> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <
> struberg@yahoo.de
> > >
> > > > >> wrote:
> > > > >> >
> > > > >> > > be careful with immediate=true. You get all sorts of nasty
> side
> > > > >> effects.
> > > > >> > >
> > > > >> > > see page 92 in
> > > > >> > >
> > > > >> >
> > > > >>
> > >
> > >
> >
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> > > > >> > >
> > > > >> > >
> > > > >> > I definitely agree and understand about immediate=true, and
> guess
> > > what, i
> > > > >> > found it very useful to disable validation as instructed on page
> > 92
> > > of
> > > > >> the
> > > > >> > PDF file.
> > > > >> >
> > > > >> > clarification of my use/understanding:
> > > > >> >
> > > > >> > i am 'not' using immediate=true, when i user is to select a row
> on
> > > > >> > datatable, and then click commandbutton/link/menuitem, which
> does
> > a
> > > POST
> > > > >> of
> > > > >> > the selected row on the datatable, and bean uses the selected
> row
> > to
> > > > >> > prepare the UI for the next view that is 'rendered' via
> > > > >> > ui:include=#{bean.page}. see below and keep reading, please.
> > > > >> >
> > > > >> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> > > > >> >
> > > > >> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
> > > > >> >
> > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > >> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> > > > >> >
> > > > >> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> > > > >> >
> > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > >> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> > > > >> >
> > > > >> > actionListener="#{pf_pointOfContactController.prepareView()}"
> > > > >> >
> > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > >> >     <p:separator/>
> > > > >> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon
> > > ui-icon-newwin"
> > > > >> >
> > > > >> >
> actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> > > > >> >
> > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > >> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> > > > >> >
> > > > >> >
> > > > >>
> > >
> >
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> > > > >> >
> > >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > > >> >
> > > > >> > but, for a readonly page that has commandbutton/links to render
> a
> > > new
> > > > >> view,
> > > > >> > based on the current @Entity that is held in the JSF
> > controller/bean
> > > > >> class,
> > > > >> > i use immediate=true without issue and I think it fits/meets the
> > > > >> > occasion/requirement, because there is no need to do validation
> > > phase or
> > > > >> > update model values. see below. :)
> > > > >> >
> > > > >> >     <p:commandButton value="Browse" icon="ui-icon-search"
> > > > >> immediate="true"
> > > > >> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > >> >
> > > > >> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> > > > >> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> > > > >> > immediate="true"
> > > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > >> >
> > > > >> >
>  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> > > > >> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> > > > >> > immediate="true"
> > > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > > >> >
> > > > >> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> > > > >> >
> > > > >>
> > > > >>
> > > > >>
> > > > >> --
> > > > >>
> -------------------------------------------------------------------
> > > > >> *SCJA. José Luis Cetina*
> > > > >>
> -------------------------------------------------------------------
> > > > >>
> > > > >
> > > > >
> > >
> >
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Whats is not supported by openjpa?
El 20/10/2013 14:22, "Romain Manni-Bucau" <rm...@gmail.com> escribió:

> Openjpa clearly doesnt support today...and no Mark it can or not work by
> spec...
> Le 20 oct. 2013 18:04, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
> > Responses inline.
> >
> > El 20/10/2013 06:51, "Mark Struberg" <st...@yahoo.de> escribió:
> > >
> > > Romain, that's nowhere in the spec. Thus it must work. Really!
> >
> > If this is true, what im doing wrong? Or this is openjpa or tomee issue?
> >
> > >
> > > The only thing which is specified to be immutable are lists returned by
> > query.getResultList.
> > > That's the reason why you should not back a sortable h:dataTable by a
> > list you get from JPA directly.
> > > All other stuff is perfectly mutable.
> > >
> > > LieGrue,
> > > strub
> > >
> > >
> > >
> > >
> > > >________________________________
> > > > From: Romain Manni-Bucau <rm...@gmail.com>
> > > >To: users@tomee.apache.org
> > > >Sent: Sunday, 20 October 2013, 10:00
> > > >Subject: Re: Entity cant be refreshed with new list values
> > > >
> > > >
> > > >Not really. An entity handles a state which can prevent it. Nothing
> > > >mandates it to work
> > > >
> > > >Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a
> > écrit
> > :
> > > >
> > > >> What about using a detached entity?? The detached entity will work
> > like a
> > > >> DTO?
> > > >>
> > > >> From Real World Java EE Patterns (Adam Biem) 2009
> > > >> Problem
> > > >> The origin problem statement was: “You want to transfer multiple
> data
> > > >> elements over a tier”
> > > >> (
> > > >>
> > > >>
> >
> >
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> > > >> ).
> > > >> This particular problem was elegantly solved in Java EE 5 with
> > detachment
> > > >> of persistent entities. There is
> > > >> no need for the introduction of another class just for transferring
> > the
> > > >> entities data. JPA entities can
> > > >> implement a java.io.Serializable interface and be transferred
> between
> > > >> tiers, even remote ones.
> > > >> CMP 2.x entities weren’t Serializable, the developer was forced to
> > copy
> > > >> their states to a remotely
> > > >> transferable structure—the Transfer Object.
> > > >>
> > > >>
> > > >>
> > > >> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
> > > >>
> > > >> > responses below...
> > > >> >
> > > >> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <struberg@yahoo.de
> >
> > > >> wrote:
> > > >> >
> > > >> > > be careful with immediate=true. You get all sorts of nasty side
> > > >> effects.
> > > >> > >
> > > >> > > see page 92 in
> > > >> > >
> > > >> >
> > > >>
> >
> >
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> > > >> > >
> > > >> > >
> > > >> > I definitely agree and understand about immediate=true, and guess
> > what, i
> > > >> > found it very useful to disable validation as instructed on page
> 92
> > of
> > > >> the
> > > >> > PDF file.
> > > >> >
> > > >> > clarification of my use/understanding:
> > > >> >
> > > >> > i am 'not' using immediate=true, when i user is to select a row on
> > > >> > datatable, and then click commandbutton/link/menuitem, which does
> a
> > POST
> > > >> of
> > > >> > the selected row on the datatable, and bean uses the selected row
> to
> > > >> > prepare the UI for the next view that is 'rendered' via
> > > >> > ui:include=#{bean.page}. see below and keep reading, please.
> > > >> >
> > > >> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> > > >> >
> > > >> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
> > > >> >
> >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > >> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> > > >> >
> > > >> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> > > >> >
> >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > >> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> > > >> >
> > > >> > actionListener="#{pf_pointOfContactController.prepareView()}"
> > > >> >
> >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > >> >     <p:separator/>
> > > >> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon
> > ui-icon-newwin"
> > > >> >
> > > >> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> > > >> >
> >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > >> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> > > >> >
> > > >> >
> > > >>
> >
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> > > >> >
> >  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > > >> >
> > > >> > but, for a readonly page that has commandbutton/links to render a
> > new
> > > >> view,
> > > >> > based on the current @Entity that is held in the JSF
> controller/bean
> > > >> class,
> > > >> > i use immediate=true without issue and I think it fits/meets the
> > > >> > occasion/requirement, because there is no need to do validation
> > phase or
> > > >> > update model values. see below. :)
> > > >> >
> > > >> >     <p:commandButton value="Browse" icon="ui-icon-search"
> > > >> immediate="true"
> > > >> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > >> >
> > > >> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> > > >> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> > > >> > immediate="true"
> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > >> >
> > > >> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> > > >> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> > > >> > immediate="true"
> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > > >> >
> > > >> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> > > >> >
> > > >>
> > > >>
> > > >>
> > > >> --
> > > >> -------------------------------------------------------------------
> > > >> *SCJA. José Luis Cetina*
> > > >> -------------------------------------------------------------------
> > > >>
> > > >
> > > >
> >
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Openjpa clearly doesnt support today...and no Mark it can or not work by
spec...
Le 20 oct. 2013 18:04, "José Luis Cetina" <ma...@gmail.com> a écrit :

> Responses inline.
>
> El 20/10/2013 06:51, "Mark Struberg" <st...@yahoo.de> escribió:
> >
> > Romain, that's nowhere in the spec. Thus it must work. Really!
>
> If this is true, what im doing wrong? Or this is openjpa or tomee issue?
>
> >
> > The only thing which is specified to be immutable are lists returned by
> query.getResultList.
> > That's the reason why you should not back a sortable h:dataTable by a
> list you get from JPA directly.
> > All other stuff is perfectly mutable.
> >
> > LieGrue,
> > strub
> >
> >
> >
> >
> > >________________________________
> > > From: Romain Manni-Bucau <rm...@gmail.com>
> > >To: users@tomee.apache.org
> > >Sent: Sunday, 20 October 2013, 10:00
> > >Subject: Re: Entity cant be refreshed with new list values
> > >
> > >
> > >Not really. An entity handles a state which can prevent it. Nothing
> > >mandates it to work
> > >
> > >Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a
> écrit
> :
> > >
> > >> What about using a detached entity?? The detached entity will work
> like a
> > >> DTO?
> > >>
> > >> From Real World Java EE Patterns (Adam Biem) 2009
> > >> Problem
> > >> The origin problem statement was: “You want to transfer multiple data
> > >> elements over a tier”
> > >> (
> > >>
> > >>
>
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> > >> ).
> > >> This particular problem was elegantly solved in Java EE 5 with
> detachment
> > >> of persistent entities. There is
> > >> no need for the introduction of another class just for transferring
> the
> > >> entities data. JPA entities can
> > >> implement a java.io.Serializable interface and be transferred between
> > >> tiers, even remote ones.
> > >> CMP 2.x entities weren’t Serializable, the developer was forced to
> copy
> > >> their states to a remotely
> > >> transferable structure—the Transfer Object.
> > >>
> > >>
> > >>
> > >> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
> > >>
> > >> > responses below...
> > >> >
> > >> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de>
> > >> wrote:
> > >> >
> > >> > > be careful with immediate=true. You get all sorts of nasty side
> > >> effects.
> > >> > >
> > >> > > see page 92 in
> > >> > >
> > >> >
> > >>
>
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> > >> > >
> > >> > >
> > >> > I definitely agree and understand about immediate=true, and guess
> what, i
> > >> > found it very useful to disable validation as instructed on page 92
> of
> > >> the
> > >> > PDF file.
> > >> >
> > >> > clarification of my use/understanding:
> > >> >
> > >> > i am 'not' using immediate=true, when i user is to select a row on
> > >> > datatable, and then click commandbutton/link/menuitem, which does a
> POST
> > >> of
> > >> > the selected row on the datatable, and bean uses the selected row to
> > >> > prepare the UI for the next view that is 'rendered' via
> > >> > ui:include=#{bean.page}. see below and keep reading, please.
> > >> >
> > >> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> > >> >
> > >> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
> > >> >
>  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > >> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> > >> >
> > >> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> > >> >
>  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > >> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> > >> >
> > >> > actionListener="#{pf_pointOfContactController.prepareView()}"
> > >> >
>  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > >> >     <p:separator/>
> > >> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon
> ui-icon-newwin"
> > >> >
> > >> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> > >> >
>  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > >> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> > >> >
> > >> >
> > >>
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> > >> >
>  update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> > >> >
> > >> > but, for a readonly page that has commandbutton/links to render a
> new
> > >> view,
> > >> > based on the current @Entity that is held in the JSF controller/bean
> > >> class,
> > >> > i use immediate=true without issue and I think it fits/meets the
> > >> > occasion/requirement, because there is no need to do validation
> phase or
> > >> > update model values. see below. :)
> > >> >
> > >> >     <p:commandButton value="Browse" icon="ui-icon-search"
> > >> immediate="true"
> > >> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > >> >
> > >> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> > >> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> > >> > immediate="true"
> update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > >> >
> > >> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> > >> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> > >> > immediate="true"
> update="#{pf_pointOfContactController.getAjaxUpdate()}"
> > >> >
> > >> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> > >> >
> > >>
> > >>
> > >>
> > >> --
> > >> -------------------------------------------------------------------
> > >> *SCJA. José Luis Cetina*
> > >> -------------------------------------------------------------------
> > >>
> > >
> > >
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Responses inline.

El 20/10/2013 06:51, "Mark Struberg" <st...@yahoo.de> escribió:
>
> Romain, that's nowhere in the spec. Thus it must work. Really!

If this is true, what im doing wrong? Or this is openjpa or tomee issue?

>
> The only thing which is specified to be immutable are lists returned by
query.getResultList.
> That's the reason why you should not back a sortable h:dataTable by a
list you get from JPA directly.
> All other stuff is perfectly mutable.
>
> LieGrue,
> strub
>
>
>
>
> >________________________________
> > From: Romain Manni-Bucau <rm...@gmail.com>
> >To: users@tomee.apache.org
> >Sent: Sunday, 20 October 2013, 10:00
> >Subject: Re: Entity cant be refreshed with new list values
> >
> >
> >Not really. An entity handles a state which can prevent it. Nothing
> >mandates it to work
> >
> >Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a écrit
:
> >
> >> What about using a detached entity?? The detached entity will work
like a
> >> DTO?
> >>
> >> From Real World Java EE Patterns (Adam Biem) 2009
> >> Problem
> >> The origin problem statement was: “You want to transfer multiple data
> >> elements over a tier”
> >> (
> >>
> >>
http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> >> ).
> >> This particular problem was elegantly solved in Java EE 5 with
detachment
> >> of persistent entities. There is
> >> no need for the introduction of another class just for transferring the
> >> entities data. JPA entities can
> >> implement a java.io.Serializable interface and be transferred between
> >> tiers, even remote ones.
> >> CMP 2.x entities weren’t Serializable, the developer was forced to copy
> >> their states to a remotely
> >> transferable structure—the Transfer Object.
> >>
> >>
> >>
> >> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
> >>
> >> > responses below...
> >> >
> >> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de>
> >> wrote:
> >> >
> >> > > be careful with immediate=true. You get all sorts of nasty side
> >> effects.
> >> > >
> >> > > see page 92 in
> >> > >
> >> >
> >>
http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> >> > >
> >> > >
> >> > I definitely agree and understand about immediate=true, and guess
what, i
> >> > found it very useful to disable validation as instructed on page 92
of
> >> the
> >> > PDF file.
> >> >
> >> > clarification of my use/understanding:
> >> >
> >> > i am 'not' using immediate=true, when i user is to select a row on
> >> > datatable, and then click commandbutton/link/menuitem, which does a
POST
> >> of
> >> > the selected row on the datatable, and bean uses the selected row to
> >> > prepare the UI for the next view that is 'rendered' via
> >> > ui:include=#{bean.page}. see below and keep reading, please.
> >> >
> >> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> >> >
> >> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
> >> >
 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> >> >
> >> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> >> >
 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> >> >
> >> > actionListener="#{pf_pointOfContactController.prepareView()}"
> >> >
 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >> >     <p:separator/>
> >> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon
ui-icon-newwin"
> >> >
> >> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> >> >
 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> >> >
> >> >
> >>
actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> >> >
 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >> >
> >> > but, for a readonly page that has commandbutton/links to render a new
> >> view,
> >> > based on the current @Entity that is held in the JSF controller/bean
> >> class,
> >> > i use immediate=true without issue and I think it fits/meets the
> >> > occasion/requirement, because there is no need to do validation
phase or
> >> > update model values. see below. :)
> >> >
> >> >     <p:commandButton value="Browse" icon="ui-icon-search"
> >> immediate="true"
> >> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >> >
> >> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> >> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> >> > immediate="true"
update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >> >
> >> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> >> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> >> > immediate="true"
update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >> >
> >> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> >> >
> >>
> >>
> >>
> >> --
> >> -------------------------------------------------------------------
> >> *SCJA. José Luis Cetina*
> >> -------------------------------------------------------------------
> >>
> >
> >

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
Another argument: what would EntityManager#merge(entity) be for if the entities would not be mutable?

LieGrue,
strub



>________________________________
> From: Mark Struberg <st...@yahoo.de>
>To: "users@tomee.apache.org" <us...@tomee.apache.org> 
>Sent: Sunday, 20 October 2013, 13:51
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>Romain, that's nowhere in the spec. Thus it must work. Really!
>
>The only thing which is specified to be immutable are lists returned by query.getResultList. 
>That's the reason why you should not back a sortable h:dataTable by a list you get from JPA directly.
>All other stuff is perfectly mutable.
>
>LieGrue,
>strub
>
>
>
>
>>________________________________
>
>> From: Romain Manni-Bucau <rm...@gmail.com>
>>To: users@tomee.apache.org 
>>Sent: Sunday, 20 October 2013, 10:00
>>Subject: Re: Entity cant be refreshed with new list values
>> 
>>
>>Not really. An entity handles a state which can prevent it. Nothing
>>mandates it to work
>>
>>Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a écrit :
>>
>>> What about using a detached entity?? The detached entity will work like a
>>> DTO?
>>>
>>> From Real World Java EE Patterns (Adam Biem) 2009
>>> Problem
>>> The origin problem statement was: “You want to transfer multiple data
>>> elements over a tier”
>>> (
>>>
>>> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
>>> ).
>>> This particular problem was elegantly solved in Java EE 5 with detachment
>>> of persistent entities. There is
>>> no need for the introduction of another class just for transferring the
>>> entities data. JPA entities can
>>> implement a java.io.Serializable interface and be transferred between
>>> tiers, even remote ones.
>>> CMP 2.x entities weren’t Serializable, the developer was forced to copy
>>> their states to a remotely
>>> transferable structure—the Transfer Object.
>>>
>>>
>>>
>>> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
>>>
>>> > responses below...
>>> >
>>> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de>
>>> wrote:
>>> >
>>> > > be careful with immediate=true. You get all sorts of nasty side
>>> effects.
>>> > >
>>> > > see page 92 in
>>> > >
>>> >
>>> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
>>> > >
>>> > >
>>> > I definitely agree and understand about immediate=true, and guess what, i
>>> > found it very useful to disable validation as instructed on page 92 of
>>> the
>>> > PDF file.
>>> >
>>> > clarification of my use/understanding:
>>> >
>>> > i am 'not' using immediate=true, when i user is to select a row on
>>> > datatable, and then click commandbutton/link/menuitem, which does a POST
>>> of
>>> > the selected row on the datatable, and bean uses the selected row to
>>> > prepare the UI for the next view that is 'rendered' via
>>> > ui:include=#{bean.page}. see below and keep reading, please.
>>> >
>>> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
>>> >
>>> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
>>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>>> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
>>> >
>>> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
>>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>>> >     <p:menuitem value="View" icon="ui-icon-folder-open"
>>> >
>>> > actionListener="#{pf_pointOfContactController.prepareView()}"
>>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>>> >     <p:separator/>
>>> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon ui-icon-newwin"
>>> >
>>> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
>>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>>> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
>>> >
>>> >
>>> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
>>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>>> >
>>> > but, for a readonly page that has commandbutton/links to render a new
>>> view,
>>> > based on the current @Entity that is held in the JSF controller/bean
>>> class,
>>> > i use immediate=true without issue and I think it fits/meets the
>>> > occasion/requirement, because there is no need to do validation phase or
>>> > update model values. see below. :)
>>> >
>>> >     <p:commandButton value="Browse" icon="ui-icon-search"
>>> immediate="true"
>>> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
>>> >
>>> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
>>> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
>>> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>>> >
>>> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
>>> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
>>> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>>> >
>>> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
>>> >
>>>
>>>
>>>
>>> --
>>> -------------------------------------------------------------------
>>> *SCJA. José Luis Cetina*
>>> -------------------------------------------------------------------
>>>
>>
>>
>
>

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
Romain, that's nowhere in the spec. Thus it must work. Really!

The only thing which is specified to be immutable are lists returned by query.getResultList. 
That's the reason why you should not back a sortable h:dataTable by a list you get from JPA directly.
All other stuff is perfectly mutable.

LieGrue,
strub




>________________________________
> From: Romain Manni-Bucau <rm...@gmail.com>
>To: users@tomee.apache.org 
>Sent: Sunday, 20 October 2013, 10:00
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>Not really. An entity handles a state which can prevent it. Nothing
>mandates it to work
>
>Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
>> What about using a detached entity?? The detached entity will work like a
>> DTO?
>>
>> From Real World Java EE Patterns (Adam Biem) 2009
>> Problem
>> The origin problem statement was: “You want to transfer multiple data
>> elements over a tier”
>> (
>>
>> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
>> ).
>> This particular problem was elegantly solved in Java EE 5 with detachment
>> of persistent entities. There is
>> no need for the introduction of another class just for transferring the
>> entities data. JPA entities can
>> implement a java.io.Serializable interface and be transferred between
>> tiers, even remote ones.
>> CMP 2.x entities weren’t Serializable, the developer was forced to copy
>> their states to a remotely
>> transferable structure—the Transfer Object.
>>
>>
>>
>> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
>>
>> > responses below...
>> >
>> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de>
>> wrote:
>> >
>> > > be careful with immediate=true. You get all sorts of nasty side
>> effects.
>> > >
>> > > see page 92 in
>> > >
>> >
>> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
>> > >
>> > >
>> > I definitely agree and understand about immediate=true, and guess what, i
>> > found it very useful to disable validation as instructed on page 92 of
>> the
>> > PDF file.
>> >
>> > clarification of my use/understanding:
>> >
>> > i am 'not' using immediate=true, when i user is to select a row on
>> > datatable, and then click commandbutton/link/menuitem, which does a POST
>> of
>> > the selected row on the datatable, and bean uses the selected row to
>> > prepare the UI for the next view that is 'rendered' via
>> > ui:include=#{bean.page}. see below and keep reading, please.
>> >
>> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
>> >
>> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
>> >
>> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>> >     <p:menuitem value="View" icon="ui-icon-folder-open"
>> >
>> > actionListener="#{pf_pointOfContactController.prepareView()}"
>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>> >     <p:separator/>
>> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon ui-icon-newwin"
>> >
>> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
>> >
>> >
>> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
>> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>> >
>> > but, for a readonly page that has commandbutton/links to render a new
>> view,
>> > based on the current @Entity that is held in the JSF controller/bean
>> class,
>> > i use immediate=true without issue and I think it fits/meets the
>> > occasion/requirement, because there is no need to do validation phase or
>> > update model values. see below. :)
>> >
>> >     <p:commandButton value="Browse" icon="ui-icon-search"
>> immediate="true"
>> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
>> >
>> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
>> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
>> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>> >
>> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
>> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
>> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>> >
>> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
>> >
>>
>>
>>
>> --
>> -------------------------------------------------------------------
>> *SCJA. José Luis Cetina*
>> -------------------------------------------------------------------
>>
>
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Not really. An entity handles a state which can prevent it. Nothing
mandates it to work
Le 20 oct. 2013 09:50, "José Luis Cetina" <ma...@gmail.com> a écrit :

> What about using a detached entity?? The detached entity will work like a
> DTO?
>
> From Real World Java EE Patterns (Adam Biem) 2009
> Problem
> The origin problem statement was: “You want to transfer multiple data
> elements over a tier”
> (
>
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
> ).
> This particular problem was elegantly solved in Java EE 5 with detachment
> of persistent entities. There is
> no need for the introduction of another class just for transferring the
> entities data. JPA entities can
> implement a java.io.Serializable interface and be transferred between
> tiers, even remote ones.
> CMP 2.x entities weren’t Serializable, the developer was forced to copy
> their states to a remotely
> transferable structure—the Transfer Object.
>
>
>
> 2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>
>
> > responses below...
> >
> > On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de>
> wrote:
> >
> > > be careful with immediate=true. You get all sorts of nasty side
> effects.
> > >
> > > see page 92 in
> > >
> >
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> > >
> > >
> > I definitely agree and understand about immediate=true, and guess what, i
> > found it very useful to disable validation as instructed on page 92 of
> the
> > PDF file.
> >
> > clarification of my use/understanding:
> >
> > i am 'not' using immediate=true, when i user is to select a row on
> > datatable, and then click commandbutton/link/menuitem, which does a POST
> of
> > the selected row on the datatable, and bean uses the selected row to
> > prepare the UI for the next view that is 'rendered' via
> > ui:include=#{bean.page}. see below and keep reading, please.
> >
> >     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
> >
> > actionListener="#{pf_pointOfContactController.prepareCreate()}"
> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
> >
> > actionListener="#{pf_pointOfContactController.prepareEdit()}"
> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >     <p:menuitem value="View" icon="ui-icon-folder-open"
> >
> > actionListener="#{pf_pointOfContactController.prepareView()}"
> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >     <p:separator/>
> >     <p:menuitem value="Copy to Ordered By" icon="ui-icon ui-icon-newwin"
> >
> > actionListener="#{pf_pointOfContactController.copySelectedRows()}"
> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
> >
> >
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
> >                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
> >
> > but, for a readonly page that has commandbutton/links to render a new
> view,
> > based on the current @Entity that is held in the JSF controller/bean
> class,
> > i use immediate=true without issue and I think it fits/meets the
> > occasion/requirement, because there is no need to do validation phase or
> > update model values. see below. :)
> >
> >     <p:commandButton value="Browse" icon="ui-icon-search"
> immediate="true"
> > update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >
> >  actionListener="#{pf_pointOfContactController.prepareList()}"/>
> >     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >
> >  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
> >     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> > immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
> >
> >  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
> >
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
What about using a detached entity?? The detached entity will work like a
DTO?

>From Real World Java EE Patterns (Adam Biem) 2009
Problem
The origin problem statement was: “You want to transfer multiple data
elements over a tier”
(
http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
).
This particular problem was elegantly solved in Java EE 5 with detachment
of persistent entities. There is
no need for the introduction of another class just for transferring the
entities data. JPA entities can
implement a java.io.Serializable interface and be transferred between
tiers, even remote ones.
CMP 2.x entities weren’t Serializable, the developer was forced to copy
their states to a remotely
transferable structure—the Transfer Object.



2013/10/19 Howard W. Smith, Jr. <sm...@gmail.com>

> responses below...
>
> On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de> wrote:
>
> > be careful with immediate=true. You get all sorts of nasty side effects.
> >
> > see page 92 in
> >
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
> >
> >
> I definitely agree and understand about immediate=true, and guess what, i
> found it very useful to disable validation as instructed on page 92 of the
> PDF file.
>
> clarification of my use/understanding:
>
> i am 'not' using immediate=true, when i user is to select a row on
> datatable, and then click commandbutton/link/menuitem, which does a POST of
> the selected row on the datatable, and bean uses the selected row to
> prepare the UI for the next view that is 'rendered' via
> ui:include=#{bean.page}. see below and keep reading, please.
>
>     <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"
>
> actionListener="#{pf_pointOfContactController.prepareCreate()}"
>                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>     <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"
>
> actionListener="#{pf_pointOfContactController.prepareEdit()}"
>                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>     <p:menuitem value="View" icon="ui-icon-folder-open"
>
> actionListener="#{pf_pointOfContactController.prepareView()}"
>                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>     <p:separator/>
>     <p:menuitem value="Copy to Ordered By" icon="ui-icon ui-icon-newwin"
>
> actionListener="#{pf_pointOfContactController.copySelectedRows()}"
>                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>     <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"
>
> actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
>                 update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
>
> but, for a readonly page that has commandbutton/links to render a new view,
> based on the current @Entity that is held in the JSF controller/bean class,
> i use immediate=true without issue and I think it fits/meets the
> occasion/requirement, because there is no need to do validation phase or
> update model values. see below. :)
>
>     <p:commandButton value="Browse" icon="ui-icon-search" immediate="true"
> update="#{pf_pointOfContactController.getAjaxUpdate()}"
>
>  actionListener="#{pf_pointOfContactController.prepareList()}"/>
>     <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
> immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>
>  actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
>     <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
> immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"
>
>  actionListener="#{pf_pointOfContactController.prepareEdit()}"/>
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
responses below...

On Sat, Oct 19, 2013 at 3:46 PM, Mark Struberg <st...@yahoo.de> wrote:

> be careful with immediate=true. You get all sorts of nasty side effects.
>
> see page 92 in
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
>
>
I definitely agree and understand about immediate=true, and guess what, i
found it very useful to disable validation as instructed on page 92 of the
PDF file.

clarification of my use/understanding:

i am 'not' using immediate=true, when i user is to select a row on
datatable, and then click commandbutton/link/menuitem, which does a POST of
the selected row on the datatable, and bean uses the selected row to
prepare the UI for the next view that is 'rendered' via
ui:include=#{bean.page}. see below and keep reading, please.

    <p:menuitem value="Add" icon="ui-icon ui-icon-circle-plus"

actionListener="#{pf_pointOfContactController.prepareCreate()}"
                update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
    <p:menuitem value="Edit" icon="ui-icon ui-icon-pencil"

actionListener="#{pf_pointOfContactController.prepareEdit()}"
                update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
    <p:menuitem value="View" icon="ui-icon-folder-open"

actionListener="#{pf_pointOfContactController.prepareView()}"
                update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
    <p:separator/>
    <p:menuitem value="Copy to Ordered By" icon="ui-icon ui-icon-newwin"

actionListener="#{pf_pointOfContactController.copySelectedRows()}"
                update="#{pf_pointOfContactController.getAjaxUpdate()}"/>
    <p:menuitem value="Delete" icon="ui-icon ui-icon-trash"

actionListener="#{pf_pointOfContactController.confirmDeleteSelectedRows()}"
                update="#{pf_pointOfContactController.getAjaxUpdate()}"/>

but, for a readonly page that has commandbutton/links to render a new view,
based on the current @Entity that is held in the JSF controller/bean class,
i use immediate=true without issue and I think it fits/meets the
occasion/requirement, because there is no need to do validation phase or
update model values. see below. :)

    <p:commandButton value="Browse" icon="ui-icon-search" immediate="true"
update="#{pf_pointOfContactController.getAjaxUpdate()}"

 actionListener="#{pf_pointOfContactController.prepareList()}"/>
    <p:commandButton value="Delete" icon="ui-icon ui-icon-trash"
immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"

 actionListener="#{pf_pointOfContactController.confirmDelete()}"/>
    <p:commandButton value="Edit" icon="ui-icon ui-icon-pencil"
immediate="true" update="#{pf_pointOfContactController.getAjaxUpdate()}"

 actionListener="#{pf_pointOfContactController.prepareEdit()}"/>

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
be careful with immediate=true. You get all sorts of nasty side effects.

see page 92 in http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf

LieGrue,
strub





>________________________________
> From: "Howard W. Smith, Jr." <sm...@gmail.com>
>To: users@tomee.apache.org; Mark Struberg <st...@yahoo.de> 
>Sent: Saturday, 19 October 2013, 21:40
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>
>responses inline below...
>
>
>
>On Sat, Oct 19, 2013 at 10:49 AM, Mark Struberg <st...@yahoo.de> wrote:
>
>It depends on the application I'd say.
>>
>>Of course if you use @Stateless then you are really bound to entitymanager-per-transaction pattern, and then DTO is almost the only thing which really works.
>>
>
>
>this is definitely what i'm doing. only @Stateless @EJBs, no @Stateful @EJBs at all.
> 
>
>>If you use @Stateful + a CDI normal scope and UserTransaction, or you use CDI + @Transactional with either UserTransaction or a producer for an EntityManager then you can also use the entitymanager-per-request pattern.
>>This plays really nice together with mostly CRUD JSF2 apps where the xhtml stuff will then be able to lazy-load the data it needs.
>>
>
>
>+1 on the entitymanager-per-request-pattern  recommendation. Definitely something that I am not doing, but I think it may work with approach that I've seen recommended by some of the experts in PrimeFaces forum (@RequestScoped beans > @EJBs). please correct me if my assumption/association is incorrect.
>
>
>i definitely need to gain some experience using producers and user transaction, and @Transactional.
>
>
>
>>This has a few pros:
>>
>>* no DTO needed as you can use the entities directly in your backing beans. Usually DTOs are mostly 1:1 the data from the entities anyway for those cases.
>>* the entity will get automatically detached after the first request, any cancel on the postback will not store the state to the DB that way.
>>
>
>
><snip> 
>* this means that all the JSR-303 BeanValidation annotations from your entities also work out of the box in your JSF2 app, without having the need to do all those validations manually and maintaining them twice!
>>
></snip>
>
>
>interesting. i am using @Stateless @EJB (+DTO) and my app/pages are using BeanValidation nicely (when I want the app to do that...usually on save/update requests/posts). i definitely use immediate=true, when I don't want bean validation to do it's thing on commandbutton/link posts in/from xhtml pages.
> 
>* Save will be done by simply em.merge the entities. There is no further need to manually apply any values nor check for any locking as the information is already available in your JPA entity.
>>
>
>
>hmmm, my save/update are definitely using em.merge in @Stateless @EJBs.
>
>
>i have the following in my AbstractFacade class:
>
>
>    public void edit(T entity) {
>        // 2011-09-17 flush immediately after merge
>        getEntityManager().merge(entity);
>        getEntityManager().flush();
>    }
>
>
>and JSF controller class has the following:
>
>
>            getFacade().edit(current);
>
>
>on a page that allows enduser to edit data, I always get a managed entity.
>
>
> 
>
>>But you also need to be aware of a few cons of this approach:
>>* if you have an application which takes a few requests to the server to render all state, then this will not work. The entity is only in managed mode in the first request - all later requests will fail to load not yet touched lazy fields. This e.g. makes it impossible to use entitymanager-per-request in Vaadin apps (Vaadin is nice from an UI perspective, but wtf, this does SOOOO many round trips to the server an almost any click in the UI!)
>>
>
>
>interesting, i definitely only want one trip to server on button-click in the UI.
> 
>* if you have a remoting technology which does transfer 'exact' representations. Then you need some mapping layer anyway.
>>
>
>
><snip> 
>* if you are bound to use pure @Stateless EJBs for your services ;)
>>
></snip>
>
>
>i think that would be me. :)
> 
>
>>In this cases a DTO approach is really better usually.
>>
>
>
>agreed.
>
>
>
>
>
>

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
responses inline below...

On Sat, Oct 19, 2013 at 10:49 AM, Mark Struberg <st...@yahoo.de> wrote:

> It depends on the application I'd say.
>
> Of course if you use @Stateless then you are really bound to
> entitymanager-per-transaction pattern, and then DTO is almost the only
> thing which really works.
>

this is definitely what i'm doing. only @Stateless @EJBs, no @Stateful
@EJBs at all.


>
> If you use @Stateful + a CDI normal scope and UserTransaction, or you use
> CDI + @Transactional with either UserTransaction or a producer for an
> EntityManager then you can also use the entitymanager-per-request pattern.
> This plays really nice together with mostly CRUD JSF2 apps where the xhtml
> stuff will then be able to lazy-load the data it needs.
>

+1 on the entitymanager-per-request-pattern  recommendation. Definitely
something that I am not doing, but I think it may work with approach that
I've seen recommended by some of the experts in PrimeFaces forum
(@RequestScoped beans > @EJBs). please correct me if my
assumption/association is incorrect.

i definitely need to gain some experience using producers and user
transaction, and @Transactional.


> This has a few pros:
>
> * no DTO needed as you can use the entities directly in your backing
> beans. Usually DTOs are mostly 1:1 the data from the entities anyway for
> those cases.
> * the entity will get automatically detached after the first request, any
> cancel on the postback will not store the state to the DB that way.
>

<snip>

> * this means that all the JSR-303 BeanValidation annotations from your
> entities also work out of the box in your JSF2 app, without having the need
> to do all those validations manually and maintaining them twice!
>
</snip>

interesting. i am using @Stateless @EJB (+DTO) and my app/pages are using
BeanValidation nicely (when I want the app to do that...usually on
save/update requests/posts). i definitely use immediate=true, when I don't
want bean validation to do it's thing on commandbutton/link posts in/from
xhtml pages.


> * Save will be done by simply em.merge the entities. There is no further
> need to manually apply any values nor check for any locking as the
> information is already available in your JPA entity.
>

hmmm, my save/update are definitely using em.merge in @Stateless @EJBs.

i have the following in my AbstractFacade class:

    public void edit(T entity) {
        // 2011-09-17 flush immediately after merge
        getEntityManager().merge(entity);
        getEntityManager().flush();
    }

and JSF controller class has the following:

            getFacade().edit(current);

on a page that allows enduser to edit data, I always get a managed entity.



>
> But you also need to be aware of a few cons of this approach:
> * if you have an application which takes a few requests to the server to
> render all state, then this will not work. The entity is only in managed
> mode in the first request - all later requests will fail to load not yet
> touched lazy fields. This e.g. makes it impossible to use
> entitymanager-per-request in Vaadin apps (Vaadin is nice from an UI
> perspective, but wtf, this does SOOOO many round trips to the server an
> almost any click in the UI!)
>

interesting, i definitely only want one trip to server on button-click in
the UI.


> * if you have a remoting technology which does transfer 'exact'
> representations. Then you need some mapping layer anyway.
>

<snip>

> * if you are bound to use pure @Stateless EJBs for your services ;)
>
</snip>

i think that would be me. :)


>
> In this cases a DTO approach is really better usually.
>

agreed.

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
It depends on the application I'd say.

Of course if you use @Stateless then you are really bound to entitymanager-per-transaction pattern, and then DTO is almost the only thing which really works.

If you use @Stateful + a CDI normal scope and UserTransaction, or you use CDI + @Transactional with either UserTransaction or a producer for an EntityManager then you can also use the entitymanager-per-request pattern.
This plays really nice together with mostly CRUD JSF2 apps where the xhtml stuff will then be able to lazy-load the data it needs. 

This has a few pros:

* no DTO needed as you can use the entities directly in your backing beans. Usually DTOs are mostly 1:1 the data from the entities anyway for those cases.
* the entity will get automatically detached after the first request, any cancel on the postback will not store the state to the DB that way.
* this means that all the JSR-303 BeanValidation annotations from your entities also work out of the box in your JSF2 app, without having the need to do all those validations manually and maintaining them twice!
* Save will be done by simply em.merge the entities. There is no further need to manually apply any values nor check for any locking as the information is already available in your JPA entity.

But you also need to be aware of a few cons of this approach:
* if you have an application which takes a few requests to the server to render all state, then this will not work. The entity is only in managed mode in the first request - all later requests will fail to load not yet touched lazy fields. This e.g. makes it impossible to use entitymanager-per-request in Vaadin apps (Vaadin is nice from an UI perspective, but wtf, this does SOOOO many round trips to the server an almost any click in the UI!) 
* if you have a remoting technology which does transfer 'exact' representations. Then you need some mapping layer anyway.
* if you are bound to use pure @Stateless EJBs for your services ;)

In this cases a DTO approach is really better usually.

LieGrue,
strub




----- Original Message -----
> From: Romain Manni-Bucau <rm...@gmail.com>
> To: users@tomee.apache.org
> Cc: 
> Sent: Saturday, 19 October 2013, 14:49
> Subject: Re: Entity cant be refreshed with new list values
> 
> More i work on apps more i think jpa should be hidden of business
> code...just my opinion. Technically both works
> Le 19 oct. 2013 14:07, "Howard W. Smith, Jr." 
> <sm...@gmail.com> a
> écrit :
> 
> 
>>  wow, interesting response, (thanks) Mark! responses inline..
>> 
>> 
>>  On Sat, Oct 19, 2013 at 3:16 AM, Mark Struberg <st...@yahoo.de> 
> wrote:
>> 
>>  > Much easier solution:
>>  > mark the fields you don't like to persist as
>>  > @javax.persistence.Transient
>>  >
>> 
>>  interesting, did not know this.
>> 
>> 
>>  >
>>  > In standard EJBs you will get the entitymanager-per-transaction 
> pattern.
>>  > Which means that any lazy loading field or relation you did not touch 
> in
>>  > your EJB will remain null forever.
>>  >
>> 
>>  i learned this after some time and work'ed around this by populating
>>  lists/collections in 'other layer of app' after the EJB retrieved 
> the
>>  @Entity, where populating = use multiple other/corresponding EJBs to
>>  populate each related List/Collection of @Entity. Bad design/performance,
>>  I'm sure.
>> 
>>  'did not touch'? i think you are referring to checking size() or 
> isEmpty()
>>  on Lists/collections that belong/related to @Entity... before @Entity is
>>  returned from @EJB method. I think I read somewhere how size() and
>>  isEmpty() will retrieve the data, but now I know, you must do this inside
>>  @EJB. right?
>> 
>> 
>>  > If you are aware of this restriction, then all is fine and you can use
>>  > EJBs. Nowadays a DTO is nothing more than making sure that you touch 
> all
>>  > the fields an info you need in the other layer of your app,
>>  >
>> 
>>  It is interesting that you refer to it as a restriction. do you
>>  prefer/recommend this approach, or another approach? I will have to see if
>>  I can improve my app a bit by keeping this in mind (as I have bandwidth to
>>  do so). thanks.
>> 
>> 
>>  > Btw, you should add a
>>  >
>>  > @Version
>>  > private Integer optlock;
>>  >
>>  > to all your entities. Otherwise you will not have any locking in the
>>  > database. And of course if you use DTOs then you will also need to set
>>  this
>>  > info into your DTO and do a manual comparison in your EJBs to make 
> sure
>>  the
>>  > entitiy did not get changed in the meantime.
>>  >
>> 
>>  are you recommending this for only OpenJPA users? I'm using EclipseLink 
> and
>>  (Apache) Derby. I think I read about this before somewhere; is the
>>  'optlock' part of Java EE implementation or just for OpenJPA users?
>> 
> 

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
understood. my app is working-as-designed-and-expected, and I am not always
using this approach, but i do know when to use it...like you said earlier,
if/when it is needed in the UI.

now i know, and I can use this approach on need-only basis, thanks. :)



On Sat, Oct 19, 2013 at 11:37 AM, Mark Struberg <st...@yahoo.de> wrote:

> Nope, there is nothing in the spec which defines that it will really load
> all the entities.
> The only thing which guarantees this is by really iterating over all the
> items in the list and even touch all the lazy attributes in those items if
> you need them.
>
> LieGrue,
> strub
>
>
>
>
> ----- Original Message -----
> > From: "Howard W. Smith, Jr." <sm...@gmail.com>
> > To: users@tomee.apache.org
> > Cc:
> > Sent: Saturday, 19 October 2013, 16:29
> > Subject: Re: Entity cant be refreshed with new list values
> >
> > so isEmpty() and size() is a hack and not recommended as touching in
> > standard EJBs?
> >
> >
> >
> >
> > On Sat, Oct 19, 2013 at 10:23 AM, Romain Manni-Bucau
> > <rm...@gmail.com>wrote:
> >
> >>  IsEmpty or size hack is not portablr
> >>  Le 19 oct. 2013 15:04, "Howard W. Smith, Jr."
> > <sm...@gmail.com> a
> >>  écrit :
> >>
> >>  > I agree. I only do/use JPA inside of @EJB's. I learned to do that
> > by
> >>  using
> >>  > NetBeans, since they generate entire web app from your database.
> >>  >
> >>  > but i need to 'touch' all related List/Collections inside of
> > the 1st @EJB
> >>  > that retrieves the @Entity and returns to other layer of app. what i
> > am
> >>  > currently doing is the following:
> >>  >
> >>  > 1. Orders order = (@EJB) facade.getOrder();
> >>  >
> >>  > 2. List<Origins> origins = (@EJB)
> >>  > originFacade.getListOfOriginsForOrder(order.orderId)
> >>  >
> >>  > 3. order.setOrigins(new ArrayList<>(origins));
> >>  >
> >>  > yes, it works, but I think you (and Mark) are telling me what I
> > learned
> >>  in
> >>  > this thread... populate Orders.origins inside of
> > OrdersFacade.getOrder()
> >>  > via isEmpty() or size(). that might be a bit more efficient, but is
> > this
> >>  > the only way to 'touch' all the List/collections related to
> > @Entity
> >>  > (Orders) ?
> >>  >
> >>  > i guess i don't like the look/requirement that I have to do
> > something
> >>  like
> >>  > this below inside @EJB
> >>  >
> >>  > if (orders.getOrigins() != null &&
> > !orders.getOrigins().isEmpty()) {
> >>  >   // this is just to touch origins, now go on to the next IF block
> >>  > }
> >>  >
> >>  > if (orders.getDestinations() != null &&
> >>  > !orders.getDestinations().isEmpty()) {
> >>  >   // this is just to touch destinations, now go on to the next IF
> > block
> >>  > }
> >>  >
> >>  > ...
> >>  >
> >>  >
> >>  > On Sat, Oct 19, 2013 at 8:49 AM, Romain Manni-Bucau
> >>  > <rm...@gmail.com>wrote:
> >>  >
> >>  > > More i work on apps more i think jpa should be hidden of business
> >>  > > code...just my opinion. Technically both works
> >>  > >
> >>  >
> >>
> >
>

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
Nope, there is nothing in the spec which defines that it will really load all the entities.
The only thing which guarantees this is by really iterating over all the items in the list and even touch all the lazy attributes in those items if you need them.

LieGrue,
strub




----- Original Message -----
> From: "Howard W. Smith, Jr." <sm...@gmail.com>
> To: users@tomee.apache.org
> Cc: 
> Sent: Saturday, 19 October 2013, 16:29
> Subject: Re: Entity cant be refreshed with new list values
> 
> so isEmpty() and size() is a hack and not recommended as touching in
> standard EJBs?
> 
> 
> 
> 
> On Sat, Oct 19, 2013 at 10:23 AM, Romain Manni-Bucau
> <rm...@gmail.com>wrote:
> 
>>  IsEmpty or size hack is not portablr
>>  Le 19 oct. 2013 15:04, "Howard W. Smith, Jr." 
> <sm...@gmail.com> a
>>  écrit :
>> 
>>  > I agree. I only do/use JPA inside of @EJB's. I learned to do that 
> by
>>  using
>>  > NetBeans, since they generate entire web app from your database.
>>  >
>>  > but i need to 'touch' all related List/Collections inside of 
> the 1st @EJB
>>  > that retrieves the @Entity and returns to other layer of app. what i 
> am
>>  > currently doing is the following:
>>  >
>>  > 1. Orders order = (@EJB) facade.getOrder();
>>  >
>>  > 2. List<Origins> origins = (@EJB)
>>  > originFacade.getListOfOriginsForOrder(order.orderId)
>>  >
>>  > 3. order.setOrigins(new ArrayList<>(origins));
>>  >
>>  > yes, it works, but I think you (and Mark) are telling me what I 
> learned
>>  in
>>  > this thread... populate Orders.origins inside of 
> OrdersFacade.getOrder()
>>  > via isEmpty() or size(). that might be a bit more efficient, but is 
> this
>>  > the only way to 'touch' all the List/collections related to 
> @Entity
>>  > (Orders) ?
>>  >
>>  > i guess i don't like the look/requirement that I have to do 
> something
>>  like
>>  > this below inside @EJB
>>  >
>>  > if (orders.getOrigins() != null && 
> !orders.getOrigins().isEmpty()) {
>>  >   // this is just to touch origins, now go on to the next IF block
>>  > }
>>  >
>>  > if (orders.getDestinations() != null &&
>>  > !orders.getDestinations().isEmpty()) {
>>  >   // this is just to touch destinations, now go on to the next IF 
> block
>>  > }
>>  >
>>  > ...
>>  >
>>  >
>>  > On Sat, Oct 19, 2013 at 8:49 AM, Romain Manni-Bucau
>>  > <rm...@gmail.com>wrote:
>>  >
>>  > > More i work on apps more i think jpa should be hidden of business
>>  > > code...just my opinion. Technically both works
>>  > >
>>  >
>> 
> 

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
so isEmpty() and size() is a hack and not recommended as touching in
standard EJBs?



On Sat, Oct 19, 2013 at 10:23 AM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> IsEmpty or size hack is not portablr
> Le 19 oct. 2013 15:04, "Howard W. Smith, Jr." <sm...@gmail.com> a
> écrit :
>
> > I agree. I only do/use JPA inside of @EJB's. I learned to do that by
> using
> > NetBeans, since they generate entire web app from your database.
> >
> > but i need to 'touch' all related List/Collections inside of the 1st @EJB
> > that retrieves the @Entity and returns to other layer of app. what i am
> > currently doing is the following:
> >
> > 1. Orders order = (@EJB) facade.getOrder();
> >
> > 2. List<Origins> origins = (@EJB)
> > originFacade.getListOfOriginsForOrder(order.orderId)
> >
> > 3. order.setOrigins(new ArrayList<>(origins));
> >
> > yes, it works, but I think you (and Mark) are telling me what I learned
> in
> > this thread... populate Orders.origins inside of OrdersFacade.getOrder()
> > via isEmpty() or size(). that might be a bit more efficient, but is this
> > the only way to 'touch' all the List/collections related to @Entity
> > (Orders) ?
> >
> > i guess i don't like the look/requirement that I have to do something
> like
> > this below inside @EJB
> >
> > if (orders.getOrigins() != null && !orders.getOrigins().isEmpty()) {
> >   // this is just to touch origins, now go on to the next IF block
> > }
> >
> > if (orders.getDestinations() != null &&
> > !orders.getDestinations().isEmpty()) {
> >   // this is just to touch destinations, now go on to the next IF block
> > }
> >
> > ...
> >
> >
> > On Sat, Oct 19, 2013 at 8:49 AM, Romain Manni-Bucau
> > <rm...@gmail.com>wrote:
> >
> > > More i work on apps more i think jpa should be hidden of business
> > > code...just my opinion. Technically both works
> > >
> >
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
IsEmpty or size hack is not portablr
Le 19 oct. 2013 15:04, "Howard W. Smith, Jr." <sm...@gmail.com> a
écrit :

> I agree. I only do/use JPA inside of @EJB's. I learned to do that by using
> NetBeans, since they generate entire web app from your database.
>
> but i need to 'touch' all related List/Collections inside of the 1st @EJB
> that retrieves the @Entity and returns to other layer of app. what i am
> currently doing is the following:
>
> 1. Orders order = (@EJB) facade.getOrder();
>
> 2. List<Origins> origins = (@EJB)
> originFacade.getListOfOriginsForOrder(order.orderId)
>
> 3. order.setOrigins(new ArrayList<>(origins));
>
> yes, it works, but I think you (and Mark) are telling me what I learned in
> this thread... populate Orders.origins inside of OrdersFacade.getOrder()
> via isEmpty() or size(). that might be a bit more efficient, but is this
> the only way to 'touch' all the List/collections related to @Entity
> (Orders) ?
>
> i guess i don't like the look/requirement that I have to do something like
> this below inside @EJB
>
> if (orders.getOrigins() != null && !orders.getOrigins().isEmpty()) {
>   // this is just to touch origins, now go on to the next IF block
> }
>
> if (orders.getDestinations() != null &&
> !orders.getDestinations().isEmpty()) {
>   // this is just to touch destinations, now go on to the next IF block
> }
>
> ...
>
>
> On Sat, Oct 19, 2013 at 8:49 AM, Romain Manni-Bucau
> <rm...@gmail.com>wrote:
>
> > More i work on apps more i think jpa should be hidden of business
> > code...just my opinion. Technically both works
> >
>

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
I agree. I only do/use JPA inside of @EJB's. I learned to do that by using
NetBeans, since they generate entire web app from your database.

but i need to 'touch' all related List/Collections inside of the 1st @EJB
that retrieves the @Entity and returns to other layer of app. what i am
currently doing is the following:

1. Orders order = (@EJB) facade.getOrder();

2. List<Origins> origins = (@EJB)
originFacade.getListOfOriginsForOrder(order.orderId)

3. order.setOrigins(new ArrayList<>(origins));

yes, it works, but I think you (and Mark) are telling me what I learned in
this thread... populate Orders.origins inside of OrdersFacade.getOrder()
via isEmpty() or size(). that might be a bit more efficient, but is this
the only way to 'touch' all the List/collections related to @Entity
(Orders) ?

i guess i don't like the look/requirement that I have to do something like
this below inside @EJB

if (orders.getOrigins() != null && !orders.getOrigins().isEmpty()) {
  // this is just to touch origins, now go on to the next IF block
}

if (orders.getDestinations() != null &&
!orders.getDestinations().isEmpty()) {
  // this is just to touch destinations, now go on to the next IF block
}

...


On Sat, Oct 19, 2013 at 8:49 AM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> More i work on apps more i think jpa should be hidden of business
> code...just my opinion. Technically both works
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
More i work on apps more i think jpa should be hidden of business
code...just my opinion. Technically both works
Le 19 oct. 2013 14:07, "Howard W. Smith, Jr." <sm...@gmail.com> a
écrit :

> wow, interesting response, (thanks) Mark! responses inline..
>
>
> On Sat, Oct 19, 2013 at 3:16 AM, Mark Struberg <st...@yahoo.de> wrote:
>
> > Much easier solution:
> > mark the fields you don't like to persist as
> > @javax.persistence.Transient
> >
>
> interesting, did not know this.
>
>
> >
> > In standard EJBs you will get the entitymanager-per-transaction pattern.
> > Which means that any lazy loading field or relation you did not touch in
> > your EJB will remain null forever.
> >
>
> i learned this after some time and work'ed around this by populating
> lists/collections in 'other layer of app' after the EJB retrieved the
> @Entity, where populating = use multiple other/corresponding EJBs to
> populate each related List/Collection of @Entity. Bad design/performance,
> I'm sure.
>
> 'did not touch'? i think you are referring to checking size() or isEmpty()
> on Lists/collections that belong/related to @Entity... before @Entity is
> returned from @EJB method. I think I read somewhere how size() and
> isEmpty() will retrieve the data, but now I know, you must do this inside
> @EJB. right?
>
>
> > If you are aware of this restriction, then all is fine and you can use
> > EJBs. Nowadays a DTO is nothing more than making sure that you touch all
> > the fields an info you need in the other layer of your app,
> >
>
> It is interesting that you refer to it as a restriction. do you
> prefer/recommend this approach, or another approach? I will have to see if
> I can improve my app a bit by keeping this in mind (as I have bandwidth to
> do so). thanks.
>
>
> > Btw, you should add a
> >
> > @Version
> > private Integer optlock;
> >
> > to all your entities. Otherwise you will not have any locking in the
> > database. And of course if you use DTOs then you will also need to set
> this
> > info into your DTO and do a manual comparison in your EJBs to make sure
> the
> > entitiy did not get changed in the meantime.
> >
>
> are you recommending this for only OpenJPA users? I'm using EclipseLink and
> (Apache) Derby. I think I read about this before somewhere; is the
> 'optlock' part of Java EE implementation or just for OpenJPA users?
>

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
On Sat, Oct 19, 2013 at 11:33 AM, Mark Struberg <st...@yahoo.de> wrote:

>
> >'did not touch'? i think you are referring to checking size() or isEmpty()
> Yes, sometimes you even need to iterate through those lists and touch all
> the lazy loading fields of those 1:n entities if you know you need them in
> the frontend.
>

interesting... for some of my @Entity classes, i have used this approach,
but for the most part, I am quite amazed that this is not always necessary
for some of the @Entity classes. I am using eclipselink query results
cache, but I did some playing with that in trying to tinker/improve
performance. i saw Christopher Schultz on tomcat user list mention
something about thrashing (or abusing) the cache... (dont' remember how he
said it), so that kinda motivated me to review my use of query results
cache. there is blog out there about tuning eclipselink jpa that I
read/reviewed at least twice, i think that kinda motivated me or reinforced
my decision to use query results cache (as much as possible).

there is one huge table that can be quite slow when i'm using PrimeFaces
autocomplete to search all rows...for PointOfContact data (and related
data). i think i had to tinker the use of query results cache and size() /
isEmpty() to improve the performance.


> >It is interesting that you refer to it as a restriction. do you
>
> > prefer/recommend this approach, or another approach?
> In those cases I'd go d' accord with Romain and suggest using DTOs.
>

okay.


>
> > I think I read somewhere how size() and isEmpty() will retrieve the
>
> > data, but now I know, you must do this inside @EJB. right?
>
> Yes, you can think of an @Stateless EJB as a CDI bean which has (amongst
> others) a CDI @Transactional interceptor [1] automatically applied to it.
> What happens is kind of what I show on page 31 in [2]. Simplified, but you
> get the idea I hope.
>
>
> This makes it obvious that once the method you invoke on your (outermost)
> EJB returns, then the interceptor will commit the transaction and close the
> EntityManager again. Please note that the EntityManager you get with
> @PersistenceContext is NOT a native JPA EntityManager but just a facade
> which delegates to the _real_ EM (per Thread and EJB invocation). And this
> 'real' EM will gets closed in the Transactional interceptor, which means
> all the entities loaded by it will automatically get detached in that
> moment.
> I hope it's clear now why lazy loading and other stuff _outside_ the EJB
> does not work as expected ;)
>
> You should nontheless be able to change values in the entites and those
> changes must get reflected to the database if you later em.merge it inside
> an EJB. Oh btw, an error which also happens pretty often is that em.merge
> gets used the wrong way:
>
> SomeStuff mergedAndManagedEntity = em.merge(detachedEntity);
>
>
> the PARAMETER entity will NOT become managed! Only the return value is
> managed!
>

okay, understood. i think i have had this understanding for some time now
(while developing JSF/JavaEE app, of course).


>
>
> >are you recommending this for only OpenJPA users?
>
> > I'm using
> EclipseLink and (Apache) Derby. I think I read
>
> > about this before
> somewhere; is the 'optlock' part of
>
> > Java EE implementation or just for
> OpenJPA users?
>
> @Version is a *very* important part of the JPA spec itself.
>
> I'm not sure why, but this is something which is often missing in
> applications.
> Maybe because almost none of the JavaEE samples on the Oracle page and in
> JavaEE talks mention this and other important stuff (like the DTO and
> EntityManager details explained above).
>
>
> I've had this discussion with Adam Bien in the past (whom I enjoy sharing
> conference slots with regularly). I recommend watching his talks because
> they are very enthusiastic and show a lot of the power of JavaEE.
> Especially if you have some junior devs and like them to get the basic
> ideas and power of JavaEE or if you like to get a good overview about new
> features of the latest JavaEE version.
>
> But he also tends to over-simply his samples and leaves out lots of very
> important parts - like the Oracle/Sun docs do as well.
>
> Adams argument is pretty straight forward. Of course he knows all those
> nasty details, but if he would go into those details then he could only
> explain one or two single features of JavaEE during each sessions. And he
> would utterly confuse listeners by explaining details which they do not get
> anyway. I can completely understand his position, but I believe for
> creating *real* applications, you need to know about all those gory details
> :/
> That's the reason why I regularly do 'Pitfalls in JavaEE' kind of lectures
> on javaeesummit and other events.
>

I have read some of Adam Bien's blog post, and I recognized all what you
mentioned above, and no respect to him, of course. Before I began Java EE
development, I studied the Java EE 6 tutorial from beginning to end (I
think I skipped the last 2 or 3 sections, or left them as
for-later-reference only), i gained my fundamental knowledge of Java EE via
that tutorial.

after migrating my app to CDI and TomEE, i learned a lot more from you all
(Apache committers)!


>
>
> Oki, back to @Version.
> This is what provides you with locking.
> There are 2 ways of locking in the database
>
> a.) pessimistic locking, also known as database row locking (if you are
> happy it only locks the row in a table. Could also lock the whole table
> depending on indexes, etc). This requires an open database connection, so
> nothing you usually like to do in a web application. See JPA
> LockMode.PESSIMISTIC_READ, etc;
>
> b.) optimistic locking. This is what you usually use in web apps. There is
> no database locking involved. Imagine you have a CUSTOMER table with the
> following columns:
> * id* name
> * birthdate
>

definitely interesting...i read about this on oracle documents site, i
think, and I think i vaguely remember @Version. definitely is good to know.
also, when i read about pessimistic and optimistic locking, it left me a
bit confused. honestly, i would love to have row locking, and I thought
about Derby's implementation/option of row-level locking, but it seems as
though my app is locking entire table during (any/every JPA) transaction.

honestly, i think i have improved the performance of most of the
app...except database access. endusers are 'not' complaining at all, but i
love tuning performance, and i know it can be improved.


>
> Now imagine a secretary A opens customer 'Jack' with id=1 and changes the
> name to 'Jill'. He lets the edit page open for around 4 hourse because he
> went to lunch. In the meantime another secretary B opened the same row,
> changed the birthdate and stores it. Finally secretary A saves his edit
> page. What happens is
>
> UPDATE CUSTOMER set name='Jill', birthdate='old' where id =1;
> which means that all the changes done by secretary B get overwritten and
> are ultimately lost!
>

i have definitely experienced this in my web app, and sometimes, i instruct
endusers to 'not' touch same record/row, if you see (or know) another
enduser is modifying that data. honestly, this does not happen much,
because it is quite seldom that endusers are working on the same data, but
sometimes, it does/can happen. And for that reason, I definitely need to
start using @Version.


>
> With optimistic locking you would not have this problem. You just need an
> additional 'version' integer column
>
> * id
> * version
> * name
> * birthdate
>
> and the update performed by JPA will look like this:
>
> UPDATE CUSTOMER set name='Jill', birthdate='old', version=:oldversion+1
> where id = 1 and version = :oldversion;
>
> where the oldversion is the version number (e.g. 1) at the time you did
> load it from the database.
> If anyone has updated the row and incremented the version in the meantime
> then your update will fail and you could detect this as
> OptimisticLockException and force the 2nd user to first reload the values
> from the DB again and then apply his changes on top of the new data. That
> way you do not loose information.
>

very interesting, thanks!


>
> I've now uploaded one of my half day lectures to my people account [3].
> Hope you can find some valuable info in there
>
>
>
> [1] http://deltaspike.apache.org/jpa.html
>
> [2]
> http://people.apache.org/~struberg/jax2013/cdi-deep-dive_1.1_jax2013.pdf
> [3]
> http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf
>
>
will definitely have to take a look at those links/references, thanks!

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
>'did not touch'? i think you are referring to checking size() or isEmpty()
Yes, sometimes you even need to iterate through those lists and touch all the lazy loading fields of those 1:n entities if you know you need them in the frontend.



>It is interesting that you refer to it as a restriction. do you 

> prefer/recommend this approach, or another approach?
In those cases I'd go d' accord with Romain and suggest using DTOs.



> I think I read somewhere how size() and isEmpty() will retrieve the 

> data, but now I know, you must do this inside @EJB. right?

Yes, you can think of an @Stateless EJB as a CDI bean which has (amongst others) a CDI @Transactional interceptor [1] automatically applied to it. What happens is kind of what I show on page 31 in [2]. Simplified, but you get the idea I hope. 


This makes it obvious that once the method you invoke on your (outermost) EJB returns, then the interceptor will commit the transaction and close the EntityManager again. Please note that the EntityManager you get with @PersistenceContext is NOT a native JPA EntityManager but just a facade which delegates to the _real_ EM (per Thread and EJB invocation). And this 'real' EM will gets closed in the Transactional interceptor, which means all the entities loaded by it will automatically get detached in that moment. 
I hope it's clear now why lazy loading and other stuff _outside_ the EJB does not work as expected ;)

You should nontheless be able to change values in the entites and those changes must get reflected to the database if you later em.merge it inside an EJB. Oh btw, an error which also happens pretty often is that em.merge gets used the wrong way:

SomeStuff mergedAndManagedEntity = em.merge(detachedEntity);


the PARAMETER entity will NOT become managed! Only the return value is managed!


>are you recommending this for only OpenJPA users? 

> I'm using 
EclipseLink and (Apache) Derby. I think I read 

> about this before 
somewhere; is the 'optlock' part of 

> Java EE implementation or just for 
OpenJPA users?

@Version is a *very* important part of the JPA spec itself. 

I'm not sure why, but this is something which is often missing in applications.
Maybe because almost none of the JavaEE samples on the Oracle page and in JavaEE talks mention this and other important stuff (like the DTO and EntityManager details explained above). 


I've had this discussion with Adam Bien in the past (whom I enjoy sharing conference slots with regularly). I recommend watching his talks because they are very enthusiastic and show a lot of the power of JavaEE. Especially if you have some junior devs and like them to get the basic ideas and power of JavaEE or if you like to get a good overview about new features of the latest JavaEE version.

But he also tends to over-simply his samples and leaves out lots of very important parts - like the Oracle/Sun docs do as well. 

Adams argument is pretty straight forward. Of course he knows all those nasty details, but if he would go into those details then he could only explain one or two single features of JavaEE during each sessions. And he would utterly confuse listeners by explaining details which they do not get anyway. I can completely understand his position, but I believe for creating *real* applications, you need to know about all those gory details :/
That's the reason why I regularly do 'Pitfalls in JavaEE' kind of lectures on javaeesummit and other events.


Oki, back to @Version.
This is what provides you with locking.
There are 2 ways of locking in the database

a.) pessimistic locking, also known as database row locking (if you are happy it only locks the row in a table. Could also lock the whole table depending on indexes, etc). This requires an open database connection, so nothing you usually like to do in a web application. See JPA LockMode.PESSIMISTIC_READ, etc;

b.) optimistic locking. This is what you usually use in web apps. There is no database locking involved. Imagine you have a CUSTOMER table with the following columns:
* id* name
* birthdate

Now imagine a secretary A opens customer 'Jack' with id=1 and changes the name to 'Jill'. He lets the edit page open for around 4 hourse because he went to lunch. In the meantime another secretary B opened the same row, changed the birthdate and stores it. Finally secretary A saves his edit page. What happens is 

UPDATE CUSTOMER set name='Jill', birthdate='old' where id =1;
which means that all the changes done by secretary B get overwritten and are ultimately lost!

With optimistic locking you would not have this problem. You just need an additional 'version' integer column

* id
* version
* name
* birthdate

and the update performed by JPA will look like this:

UPDATE CUSTOMER set name='Jill', birthdate='old', version=:oldversion+1 where id = 1 and version = :oldversion;

where the oldversion is the version number (e.g. 1) at the time you did load it from the database.
If anyone has updated the row and incremented the version in the meantime then your update will fail and you could detect this as OptimisticLockException and force the 2nd user to first reload the values from the DB again and then apply his changes on top of the new data. That way you do not loose information.

I've now uploaded one of my half day lectures to my people account [3]. Hope you can find some valuable info in there



[1] http://deltaspike.apache.org/jpa.html

[2] http://people.apache.org/~struberg/jax2013/cdi-deep-dive_1.1_jax2013.pdf
[3] http://people.apache.org/~struberg/eesummit2013/Java%20EE%20Summit%20-%20pitfalls%20in%20EE.pdf



>________________________________
> From: "Howard W. Smith, Jr." <sm...@gmail.com>
>To: users@tomee.apache.org; Mark Struberg <st...@yahoo.de> 
>Sent: Saturday, 19 October 2013, 14:06
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>
>wow, interesting response, (thanks) Mark! responses inline..
>
>
>
>
>On Sat, Oct 19, 2013 at 3:16 AM, Mark Struberg <st...@yahoo.de> wrote:
>
>Much easier solution:
>>mark the fields you don't like to persist as
>>@javax.persistence.Transient
>>
>
>
>interesting, did not know this.
> 
>
>>In standard EJBs you will get the entitymanager-per-transaction pattern. Which means that any lazy loading field or relation you did not touch in your EJB will remain null forever.
>>
>
>
>i learned this after some time and work'ed around this by populating lists/collections in 'other layer of app' after the EJB retrieved the @Entity, where populating = use multiple other/corresponding EJBs to populate each related List/Collection of @Entity. Bad design/performance, I'm sure.
>
>
>'did not touch'? i think you are referring to checking size() or isEmpty() on Lists/collections that belong/related to @Entity... before @Entity is returned from @EJB method. I think I read somewhere how size() and isEmpty() will retrieve the data, but now I know, you must do this inside @EJB. right?
> 
>If you are aware of this restriction, then all is fine and you can use EJBs. Nowadays a DTO is nothing more than making sure that you touch all the fields an info you need in the other layer of your app,
>>
>
>
>It is interesting that you refer to it as a restriction. do you prefer/recommend this approach, or another approach? I will have to see if I can improve my app a bit by keeping this in mind (as I have bandwidth to do so). thanks.
>
>
>
>>Btw, you should add a
>>
>>@Version
>>private Integer optlock;
>>
>>to all your entities. Otherwise you will not have any locking in the database. And of course if you use DTOs then you will also need to set this info into your DTO and do a manual comparison in your EJBs to make sure the entitiy did not get changed in the meantime.
>>
>
>
>are you recommending this for only OpenJPA users? I'm using EclipseLink and (Apache) Derby. I think I read about this before somewhere; is the 'optlock' part of Java EE implementation or just for OpenJPA users?
>
>
>
>
>
>

Re: Entity cant be refreshed with new list values

Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
wow, interesting response, (thanks) Mark! responses inline..


On Sat, Oct 19, 2013 at 3:16 AM, Mark Struberg <st...@yahoo.de> wrote:

> Much easier solution:
> mark the fields you don't like to persist as
> @javax.persistence.Transient
>

interesting, did not know this.


>
> In standard EJBs you will get the entitymanager-per-transaction pattern.
> Which means that any lazy loading field or relation you did not touch in
> your EJB will remain null forever.
>

i learned this after some time and work'ed around this by populating
lists/collections in 'other layer of app' after the EJB retrieved the
@Entity, where populating = use multiple other/corresponding EJBs to
populate each related List/Collection of @Entity. Bad design/performance,
I'm sure.

'did not touch'? i think you are referring to checking size() or isEmpty()
on Lists/collections that belong/related to @Entity... before @Entity is
returned from @EJB method. I think I read somewhere how size() and
isEmpty() will retrieve the data, but now I know, you must do this inside
@EJB. right?


> If you are aware of this restriction, then all is fine and you can use
> EJBs. Nowadays a DTO is nothing more than making sure that you touch all
> the fields an info you need in the other layer of your app,
>

It is interesting that you refer to it as a restriction. do you
prefer/recommend this approach, or another approach? I will have to see if
I can improve my app a bit by keeping this in mind (as I have bandwidth to
do so). thanks.


> Btw, you should add a
>
> @Version
> private Integer optlock;
>
> to all your entities. Otherwise you will not have any locking in the
> database. And of course if you use DTOs then you will also need to set this
> info into your DTO and do a manual comparison in your EJBs to make sure the
> entitiy did not get changed in the meantime.
>

are you recommending this for only OpenJPA users? I'm using EclipseLink and
(Apache) Derby. I think I read about this before somewhere; is the
'optlock' part of Java EE implementation or just for OpenJPA users?

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
Much easier solution:
mark the fields you don't like to persist as 
@javax.persistence.Transient

I still wonder why you cannot set a new array into the entity.  Imo this should work even for unmanaged entities. All which happens (at least if you use OpenJPA with enhancement and not with subclassing - which is mostly broken) is that the PCStateManagerImpl inside the entity gets replace by a DetachedStateManagerImpl which tracks the _dirty field updates.

But I agree that it's much better to use DTOs whenever you use EJBs.
I would suggest to only use not DTOs when you use either a producer for a @RequestScoped EntityManager (not container managed, so you need to use @Inject) or if you use a manually managed UserTransaction.

In standard EJBs you will get the entitymanager-per-transaction pattern. Which means that any lazy loading field or relation you did not touch in your EJB will remain null forever.
If you are aware of this restriction, then all is fine and you can use EJBs. Nowadays a DTO is nothing more than making sure that you touch all the fields an info you need in the other layer of your app,

Btw, you should add a 

@Version
private Integer optlock;

to all your entities. Otherwise you will not have any locking in the database. And of course if you use DTOs then you will also need to set this info into your DTO and do a manual comparison in your EJBs to make sure the entitiy did not get changed in the meantime. 



LieGrue,
strub



>________________________________
> From: Romain Manni-Bucau <rm...@gmail.com>
>To: users@tomee.apache.org 
>Sent: Friday, 18 October 2013, 20:15
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>Create a copy or use a dto
>
>Le 18 oct. 2013 20:06, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
>> I only want to add some info to a retrieved object but i dont want to
>> persist that new info
>>
>>
>> 2013/10/18 José Luis Cetina <ma...@gmail.com>
>>
>> > But if use a transaction the new values will be committed and i want that
>> > values as a read only
>> >
>> >
>> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>> >
>> >> Cause you arent managed...do the whole method in an ejb (with a
>> >> transaction)
>> >> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a
>> écrit
>> >> :
>> >>
>> >> > Ok, in the example i tried to set the list in the ejb, but if removed
>> >> and
>> >> > then put the list in the managed bean i get the same behavior
>> >> >
>> >> >
>> >> > @Named
>> >> > public class MyBean implements Serializable{
>> >> >
>> >> >  @EJB
>> >> >  private MyEJB ejb;
>> >> >
>> >> >  public void anyMethod(){
>> >> >    User user = ejb.getUserWithRoles();
>> >> >     //i will create a list of role just for try to set any role the
>> >> > userJohn
>> >> >       List<Role> roleList = new ArrayList<Role>(2);
>> >> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>> >> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role
>> 2
>> >> >
>> >> >      //setting the list of roles created to the user, as you can see
>> the
>> >> > list has 2 values but the value roles of userJohn always is set to
>> >> null, my
>> >> > setters and getters are correct
>> >> >      user.setRoles(roleList);
>> >> >
>> >> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
>> >> >  }
>> >> >
>> >> > }
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > I dont want these roles get persisted i just want to modify my object,
>> >> is
>> >> > still the same even setting the value in the maged bean?
>> >> >
>> >> >
>> >> >
>> >> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>> >> >
>> >> > > Hi
>> >> > >
>> >> > > Your entity is not managed so you cant do it. It is normal.
>> >> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a
>> >> > écrit :
>> >> > >
>> >> > > > I sent this mail to openJPA mailing list but i dont know if this
>> >> could
>> >> > be
>> >> > > > something related to TomEE or just im doing something wrong.
>> >> > > >
>> >> > > >
>> >> > > > I have a "strange" behavior, with list properties of any kind of
>> >> > entity.
>> >> > > >
>> >> > > > When i try to set a non empty list to a retrieved entity that has
>> a
>> >> > list
>> >> > > > value with null (because is not fetched) the entity doesnt
>> "reflect"
>> >> > the
>> >> > > > new non-empty values that i set.
>> >> > > >
>> >> > > > Steps
>> >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> >> > criterias.
>> >> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
>> >> default is
>> >> > > > lazy thats why if i dont do a fetch (i dont) the list is null
>> >> > > > 3. Then try to fill the role list with a NON-EMPTY list
>> >> > > > 4. Here the list that i set has values but the user list has a
>> null
>> >> in
>> >> > > the
>> >> > > > roles list attribute even though i set it in the step 3
>> >> > > >
>> >> > > >
>> >> > > > I dont know why this is happening, the only way i could fix this
>> is
>> >> > > cloning
>> >> > > > (using SerializationUtils from Apache commons.lang3) the user
>> entity
>> >> > (see
>> >> > > > the steps) and with this work, but i dont know why.
>> >> > > >
>> >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> >> > criterias.
>> >> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
>> >> > > > SerializationUtils.clone(originalRetrivedUser);
>> >> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
>> >> > clonedUser
>> >> > > > 4. The list of clonedUser has the correct values
>> >> > > >
>> >> > > >
>> >> > > > Why i need to clone? why i cannot set a list to the entity if is
>> not
>> >> > > > cloned?
>> >> > > >
>> >> > > >
>> >> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
>> >> > embedded)
>> >> > > >
>> >> > > >
>> >> > > > Scenario:
>> >> > > >
>> >> > > > ***** ENTITIES *****
>> >> > > > @Entity
>> >> > > > public class User implements Serializable{
>> >> > > >
>> >> > > >    @Id
>> >> > > >    private int id;
>> >> > > >    private String userName;
>> >> > > >    private String password;
>> >> > > >    @OneToMany(mappedBy = "user")
>> >> > > >    private List<Role> roles;
>> >> > > >
>> >> > > >    //getters and setters..
>> >> > > >
>> >> > > > }
>> >> > > >
>> >> > > >
>> >> > > > @Entity
>> >> > > > public class Role implements Serializable{
>> >> > > >
>> >> > > >    @Id
>> >> > > >    private int id;
>> >> > > >    private String roleName;
>> >> > > >    @ManyToOne
>> >> > > >    @JoinColumn(name = "user_id")
>> >> > > >    private User user;
>> >> > > >
>> >> > > >   //getters and setters..
>> >> > > > }
>> >> > > >
>> >> > > >
>> >> > > > **** EJB CLASS ****
>> >> > > > @Stateless
>> >> > > > public class MyEJB{
>> >> > > >
>> >> > > >     @PersistenceContext(unitName ="ANY_NAME")
>> >> > > >     private EntityManager em;
>> >> > > >
>> >> > > >   public User getUserWithRoles(){
>> >> > > >
>> >> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
>> >> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
>> >> > > >         Root<User> root = cq.from(User.class);
>> >> > > >
>> >> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
>> >> > > >
>> >> > > >        User userJohn = em.createQuery(cq).getSingleResult();  //
>> if
>> >> i
>> >> > > want
>> >> > > > this work i have to do User userJohn =
>> >> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());
>> >>  [1*]
>> >> > > >
>> >> > > >
>> >> > > >        //i will create a list of role just for try to set any role
>> >> the
>> >> > > > userJohn
>> >> > > >       List<Role> roleList = new ArrayList<Role>(2);
>> >> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding
>> role 1
>> >> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
>> >> role 2
>> >> > > >
>> >> > > >      //setting the list of roles created to the user, as you can
>> see
>> >> > the
>> >> > > > list has 2 values but the value roles of userJohn always is set to
>> >> > null,
>> >> > > my
>> >> > > > setters and getters are correct
>> >> > > >      userJohn.setRoles(roleList);
>> >> > > >
>> >> > > >      return userJohn;
>> >> > > >   }
>> >> > > >
>> >> > > > }
>> >> > > >
>> >> > > > *** MANAGED BEAN ****
>> >> > > >
>> >> > > > @Named
>> >> > > > public class MyBean implements Serializable{
>> >> > > >
>> >> > > >  @EJB
>> >> > > >  private MyEJB ejb;
>> >> > > >
>> >> > > >  public void anyMethod(){
>> >> > > >    User user = ejb.getUserWithRoles();
>> >> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i
>> >> use
>> >> > > > clone in the ejb method (see 1*) i can get the corrected values.
>> >> > > >
>> >> > > >  }
>> >> > > >
>> >> > > >
>> >> > > > }
>> >> > > >
>> >> > > > I know i can get the values fetching but im trying to not do it
>> >> > > >
>> >> > >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > -------------------------------------------------------------------
>> >> > *SCJA. José Luis Cetina*
>> >> > -------------------------------------------------------------------
>> >> >
>> >>
>> >
>> >
>> >
>> > --
>> > -------------------------------------------------------------------
>> > *SCJA. José Luis Cetina*
>> > -------------------------------------------------------------------
>> >
>>
>>
>>
>> --
>> -------------------------------------------------------------------
>> *SCJA. José Luis Cetina*
>> -------------------------------------------------------------------
>>
>
>

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Mark, the enhancement in detached forces cloning. In all case dti pattern
is not that bad
Le 19 oct. 2013 09:18, "Mark Struberg" <st...@yahoo.de> a écrit :

> Clone is not a good solution imo.
> What kind of enhancement do you use?
>
> LieGrue,
> strub
>
>
>
>
> >________________________________
> > From: José Luis Cetina <ma...@gmail.com>
> >To: users@tomee.apache.org
> >Sent: Friday, 18 October 2013, 21:57
> >Subject: Re: Entity cant be refreshed with new list values
> >
> >
> >Thanks Romain, i tried using detach but i get the same behavior, with
> clone
> >is the only way that i could this working
> >
> >
> >
> >2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >
> >> Create a copy or use a dto
> >> Le 18 oct. 2013 20:06, "José Luis Cetina" <ma...@gmail.com> a
> écrit :
> >>
> >> > I only want to add some info to a retrieved object but i dont want to
> >> > persist that new info
> >> >
> >> >
> >> > 2013/10/18 José Luis Cetina <ma...@gmail.com>
> >> >
> >> > > But if use a transaction the new values will be committed and i want
> >> that
> >> > > values as a read only
> >> > >
> >> > >
> >> > > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >> > >
> >> > >> Cause you arent managed...do the whole method in an ejb (with a
> >> > >> transaction)
> >> > >> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a
> >> > écrit
> >> > >> :
> >> > >>
> >> > >> > Ok, in the example i tried to set the list in the ejb, but if
> >> removed
> >> > >> and
> >> > >> > then put the list in the managed bean i get the same behavior
> >> > >> >
> >> > >> >
> >> > >> > @Named
> >> > >> > public class MyBean implements Serializable{
> >> > >> >
> >> > >> >  @EJB
> >> > >> >  private MyEJB ejb;
> >> > >> >
> >> > >> >  public void anyMethod(){
> >> > >> >    User user = ejb.getUserWithRoles();
> >> > >> >     //i will create a list of role just for try to set any role
> the
> >> > >> > userJohn
> >> > >> >       List<Role> roleList = new ArrayList<Role>(2);
> >> > >> >       roleList.add(new Role(1,'ADMIN'); //creating and adding
> role 1
> >> > >> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
> >> role
> >> > 2
> >> > >> >
> >> > >> >      //setting the list of roles created to the user, as you can
> see
> >> > the
> >> > >> > list has 2 values but the value roles of userJohn always is set
> to
> >> > >> null, my
> >> > >> > setters and getters are correct
> >> > >> >      user.setRoles(roleList);
> >> > >> >
> >> > >> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
> >> > >> >  }
> >> > >> >
> >> > >> > }
> >> > >> >
> >> > >> >
> >> > >> >
> >> > >> >
> >> > >> > I dont want these roles get persisted i just want to modify my
> >> object,
> >> > >> is
> >> > >> > still the same even setting the value in the maged bean?
> >> > >> >
> >> > >> >
> >> > >> >
> >> > >> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >> > >> >
> >> > >> > > Hi
> >> > >> > >
> >> > >> > > Your entity is not managed so you cant do it. It is normal.
> >> > >> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <
> maxtorzito@gmail.com>
> >> a
> >> > >> > écrit :
> >> > >> > >
> >> > >> > > > I sent this mail to openJPA mailing list but i dont know if
> this
> >> > >> could
> >> > >> > be
> >> > >> > > > something related to TomEE or just im doing something wrong.
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > I have a "strange" behavior, with list properties of any
> kind of
> >> > >> > entity.
> >> > >> > > >
> >> > >> > > > When i try to set a non empty list to a retrieved entity that
> >> has
> >> > a
> >> > >> > list
> >> > >> > > > value with null (because is not fetched) the entity doesnt
> >> > "reflect"
> >> > >> > the
> >> > >> > > > new non-empty values that i set.
> >> > >> > > >
> >> > >> > > > Steps
> >> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb
> with
> >> > >> > criterias.
> >> > >> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
> >> > >> default is
> >> > >> > > > lazy thats why if i dont do a fetch (i dont) the list is null
> >> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list
> >> > >> > > > 4. Here the list that i set has values but the user list has
> a
> >> > null
> >> > >> in
> >> > >> > > the
> >> > >> > > > roles list attribute even though i set it in the step 3
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > I dont know why this is happening, the only way i could fix
> this
> >> > is
> >> > >> > > cloning
> >> > >> > > > (using SerializationUtils from Apache commons.lang3) the user
> >> > entity
> >> > >> > (see
> >> > >> > > > the steps) and with this work, but i dont know why.
> >> > >> > > >
> >> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb
> with
> >> > >> > criterias.
> >> > >> > > > 2. Clone retrieved entity and asig to new one User
> clonedUser =
> >> > >> > > > SerializationUtils.clone(originalRetrivedUser);
> >> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list to
> the
> >> > >> > clonedUser
> >> > >> > > > 4. The list of clonedUser has the correct values
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > Why i need to clone? why i cannot set a list to the entity
> if is
> >> > not
> >> > >> > > > cloned?
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa
> version
> >> > >> > embedded)
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > Scenario:
> >> > >> > > >
> >> > >> > > > ***** ENTITIES *****
> >> > >> > > > @Entity
> >> > >> > > > public class User implements Serializable{
> >> > >> > > >
> >> > >> > > >    @Id
> >> > >> > > >    private int id;
> >> > >> > > >    private String userName;
> >> > >> > > >    private String password;
> >> > >> > > >    @OneToMany(mappedBy = "user")
> >> > >> > > >    private List<Role> roles;
> >> > >> > > >
> >> > >> > > >    //getters and setters..
> >> > >> > > >
> >> > >> > > > }
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > @Entity
> >> > >> > > > public class Role implements Serializable{
> >> > >> > > >
> >> > >> > > >    @Id
> >> > >> > > >    private int id;
> >> > >> > > >    private String roleName;
> >> > >> > > >    @ManyToOne
> >> > >> > > >    @JoinColumn(name = "user_id")
> >> > >> > > >    private User user;
> >> > >> > > >
> >> > >> > > >   //getters and setters..
> >> > >> > > > }
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > **** EJB CLASS ****
> >> > >> > > > @Stateless
> >> > >> > > > public class MyEJB{
> >> > >> > > >
> >> > >> > > >     @PersistenceContext(unitName ="ANY_NAME")
> >> > >> > > >     private EntityManager em;
> >> > >> > > >
> >> > >> > > >   public User getUserWithRoles(){
> >> > >> > > >
> >> > >> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
> >> > >> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> >> > >> > > >         Root<User> root = cq.from(User.class);
> >> > >> > > >
> >> > >> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
> >> > >> > > >
> >> > >> > > >        User userJohn =
> em.createQuery(cq).getSingleResult();  //
> >> > if
> >> > >> i
> >> > >> > > want
> >> > >> > > > this work i have to do User userJohn =
> >> > >> > > >
> SerializationUtils.clone(em.createQuery(cq).getSingleResult());
> >> > >>  [1*]
> >> > >> > > >
> >> > >> > > >
> >> > >> > > >        //i will create a list of role just for try to set any
> >> role
> >> > >> the
> >> > >> > > > userJohn
> >> > >> > > >       List<Role> roleList = new ArrayList<Role>(2);
> >> > >> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding
> >> > role 1
> >> > >> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and
> adding
> >> > >> role 2
> >> > >> > > >
> >> > >> > > >      //setting the list of roles created to the user, as you
> can
> >> > see
> >> > >> > the
> >> > >> > > > list has 2 values but the value roles of userJohn always is
> set
> >> to
> >> > >> > null,
> >> > >> > > my
> >> > >> > > > setters and getters are correct
> >> > >> > > >      userJohn.setRoles(roleList);
> >> > >> > > >
> >> > >> > > >      return userJohn;
> >> > >> > > >   }
> >> > >> > > >
> >> > >> > > > }
> >> > >> > > >
> >> > >> > > > *** MANAGED BEAN ****
> >> > >> > > >
> >> > >> > > > @Named
> >> > >> > > > public class MyBean implements Serializable{
> >> > >> > > >
> >> > >> > > >  @EJB
> >> > >> > > >  private MyEJB ejb;
> >> > >> > > >
> >> > >> > > >  public void anyMethod(){
> >> > >> > > >    User user = ejb.getUserWithRoles();
> >> > >> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but
> >> if i
> >> > >> use
> >> > >> > > > clone in the ejb method (see 1*) i can get the corrected
> values.
> >> > >> > > >
> >> > >> > > >  }
> >> > >> > > >
> >> > >> > > >
> >> > >> > > > }
> >> > >> > > >
> >> > >> > > > I know i can get the values fetching but im trying to not do
> it
> >> > >> > > >
> >> > >> > >
> >> > >> >
> >> > >> >
> >> > >> >
> >> > >> > --
> >> > >> >
> -------------------------------------------------------------------
> >> > >> > *SCJA. José Luis Cetina*
> >> > >> >
> -------------------------------------------------------------------
> >> > >> >
> >> > >>
> >> > >
> >> > >
> >> > >
> >> > > --
> >> > > -------------------------------------------------------------------
> >> > > *SCJA. José Luis Cetina*
> >> > > -------------------------------------------------------------------
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > -------------------------------------------------------------------
> >> > *SCJA. José Luis Cetina*
> >> > -------------------------------------------------------------------
> >> >
> >>
> >
> >
> >
> >--
> >-------------------------------------------------------------------
> >*SCJA. José Luis Cetina*
> >-------------------------------------------------------------------
> >
> >

Re: Entity cant be refreshed with new list values

Posted by Mark Struberg <st...@yahoo.de>.
Clone is not a good solution imo. 
What kind of enhancement do you use?

LieGrue,
strub




>________________________________
> From: José Luis Cetina <ma...@gmail.com>
>To: users@tomee.apache.org 
>Sent: Friday, 18 October 2013, 21:57
>Subject: Re: Entity cant be refreshed with new list values
> 
>
>Thanks Romain, i tried using detach but i get the same behavior, with clone
>is the only way that i could this working
>
>
>
>2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>
>> Create a copy or use a dto
>> Le 18 oct. 2013 20:06, "José Luis Cetina" <ma...@gmail.com> a écrit :
>>
>> > I only want to add some info to a retrieved object but i dont want to
>> > persist that new info
>> >
>> >
>> > 2013/10/18 José Luis Cetina <ma...@gmail.com>
>> >
>> > > But if use a transaction the new values will be committed and i want
>> that
>> > > values as a read only
>> > >
>> > >
>> > > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>> > >
>> > >> Cause you arent managed...do the whole method in an ejb (with a
>> > >> transaction)
>> > >> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a
>> > écrit
>> > >> :
>> > >>
>> > >> > Ok, in the example i tried to set the list in the ejb, but if
>> removed
>> > >> and
>> > >> > then put the list in the managed bean i get the same behavior
>> > >> >
>> > >> >
>> > >> > @Named
>> > >> > public class MyBean implements Serializable{
>> > >> >
>> > >> >  @EJB
>> > >> >  private MyEJB ejb;
>> > >> >
>> > >> >  public void anyMethod(){
>> > >> >    User user = ejb.getUserWithRoles();
>> > >> >     //i will create a list of role just for try to set any role the
>> > >> > userJohn
>> > >> >       List<Role> roleList = new ArrayList<Role>(2);
>> > >> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>> > >> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
>> role
>> > 2
>> > >> >
>> > >> >      //setting the list of roles created to the user, as you can see
>> > the
>> > >> > list has 2 values but the value roles of userJohn always is set to
>> > >> null, my
>> > >> > setters and getters are correct
>> > >> >      user.setRoles(roleList);
>> > >> >
>> > >> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
>> > >> >  }
>> > >> >
>> > >> > }
>> > >> >
>> > >> >
>> > >> >
>> > >> >
>> > >> > I dont want these roles get persisted i just want to modify my
>> object,
>> > >> is
>> > >> > still the same even setting the value in the maged bean?
>> > >> >
>> > >> >
>> > >> >
>> > >> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>> > >> >
>> > >> > > Hi
>> > >> > >
>> > >> > > Your entity is not managed so you cant do it. It is normal.
>> > >> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com>
>> a
>> > >> > écrit :
>> > >> > >
>> > >> > > > I sent this mail to openJPA mailing list but i dont know if this
>> > >> could
>> > >> > be
>> > >> > > > something related to TomEE or just im doing something wrong.
>> > >> > > >
>> > >> > > >
>> > >> > > > I have a "strange" behavior, with list properties of any kind of
>> > >> > entity.
>> > >> > > >
>> > >> > > > When i try to set a non empty list to a retrieved entity that
>> has
>> > a
>> > >> > list
>> > >> > > > value with null (because is not fetched) the entity doesnt
>> > "reflect"
>> > >> > the
>> > >> > > > new non-empty values that i set.
>> > >> > > >
>> > >> > > > Steps
>> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> > >> > criterias.
>> > >> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
>> > >> default is
>> > >> > > > lazy thats why if i dont do a fetch (i dont) the list is null
>> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list
>> > >> > > > 4. Here the list that i set has values but the user list has a
>> > null
>> > >> in
>> > >> > > the
>> > >> > > > roles list attribute even though i set it in the step 3
>> > >> > > >
>> > >> > > >
>> > >> > > > I dont know why this is happening, the only way i could fix this
>> > is
>> > >> > > cloning
>> > >> > > > (using SerializationUtils from Apache commons.lang3) the user
>> > entity
>> > >> > (see
>> > >> > > > the steps) and with this work, but i dont know why.
>> > >> > > >
>> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> > >> > criterias.
>> > >> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
>> > >> > > > SerializationUtils.clone(originalRetrivedUser);
>> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
>> > >> > clonedUser
>> > >> > > > 4. The list of clonedUser has the correct values
>> > >> > > >
>> > >> > > >
>> > >> > > > Why i need to clone? why i cannot set a list to the entity if is
>> > not
>> > >> > > > cloned?
>> > >> > > >
>> > >> > > >
>> > >> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
>> > >> > embedded)
>> > >> > > >
>> > >> > > >
>> > >> > > > Scenario:
>> > >> > > >
>> > >> > > > ***** ENTITIES *****
>> > >> > > > @Entity
>> > >> > > > public class User implements Serializable{
>> > >> > > >
>> > >> > > >    @Id
>> > >> > > >    private int id;
>> > >> > > >    private String userName;
>> > >> > > >    private String password;
>> > >> > > >    @OneToMany(mappedBy = "user")
>> > >> > > >    private List<Role> roles;
>> > >> > > >
>> > >> > > >    //getters and setters..
>> > >> > > >
>> > >> > > > }
>> > >> > > >
>> > >> > > >
>> > >> > > > @Entity
>> > >> > > > public class Role implements Serializable{
>> > >> > > >
>> > >> > > >    @Id
>> > >> > > >    private int id;
>> > >> > > >    private String roleName;
>> > >> > > >    @ManyToOne
>> > >> > > >    @JoinColumn(name = "user_id")
>> > >> > > >    private User user;
>> > >> > > >
>> > >> > > >   //getters and setters..
>> > >> > > > }
>> > >> > > >
>> > >> > > >
>> > >> > > > **** EJB CLASS ****
>> > >> > > > @Stateless
>> > >> > > > public class MyEJB{
>> > >> > > >
>> > >> > > >     @PersistenceContext(unitName ="ANY_NAME")
>> > >> > > >     private EntityManager em;
>> > >> > > >
>> > >> > > >   public User getUserWithRoles(){
>> > >> > > >
>> > >> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
>> > >> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
>> > >> > > >         Root<User> root = cq.from(User.class);
>> > >> > > >
>> > >> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
>> > >> > > >
>> > >> > > >        User userJohn = em.createQuery(cq).getSingleResult();  //
>> > if
>> > >> i
>> > >> > > want
>> > >> > > > this work i have to do User userJohn =
>> > >> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());
>> > >>  [1*]
>> > >> > > >
>> > >> > > >
>> > >> > > >        //i will create a list of role just for try to set any
>> role
>> > >> the
>> > >> > > > userJohn
>> > >> > > >       List<Role> roleList = new ArrayList<Role>(2);
>> > >> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding
>> > role 1
>> > >> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
>> > >> role 2
>> > >> > > >
>> > >> > > >      //setting the list of roles created to the user, as you can
>> > see
>> > >> > the
>> > >> > > > list has 2 values but the value roles of userJohn always is set
>> to
>> > >> > null,
>> > >> > > my
>> > >> > > > setters and getters are correct
>> > >> > > >      userJohn.setRoles(roleList);
>> > >> > > >
>> > >> > > >      return userJohn;
>> > >> > > >   }
>> > >> > > >
>> > >> > > > }
>> > >> > > >
>> > >> > > > *** MANAGED BEAN ****
>> > >> > > >
>> > >> > > > @Named
>> > >> > > > public class MyBean implements Serializable{
>> > >> > > >
>> > >> > > >  @EJB
>> > >> > > >  private MyEJB ejb;
>> > >> > > >
>> > >> > > >  public void anyMethod(){
>> > >> > > >    User user = ejb.getUserWithRoles();
>> > >> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but
>> if i
>> > >> use
>> > >> > > > clone in the ejb method (see 1*) i can get the corrected values.
>> > >> > > >
>> > >> > > >  }
>> > >> > > >
>> > >> > > >
>> > >> > > > }
>> > >> > > >
>> > >> > > > I know i can get the values fetching but im trying to not do it
>> > >> > > >
>> > >> > >
>> > >> >
>> > >> >
>> > >> >
>> > >> > --
>> > >> > -------------------------------------------------------------------
>> > >> > *SCJA. José Luis Cetina*
>> > >> > -------------------------------------------------------------------
>> > >> >
>> > >>
>> > >
>> > >
>> > >
>> > > --
>> > > -------------------------------------------------------------------
>> > > *SCJA. José Luis Cetina*
>> > > -------------------------------------------------------------------
>> > >
>> >
>> >
>> >
>> > --
>> > -------------------------------------------------------------------
>> > *SCJA. José Luis Cetina*
>> > -------------------------------------------------------------------
>> >
>>
>
>
>
>-- 
>-------------------------------------------------------------------
>*SCJA. José Luis Cetina*
>-------------------------------------------------------------------
>
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Thanks Romain, i tried using detach but i get the same behavior, with clone
is the only way that i could this working


2013/10/18 Romain Manni-Bucau <rm...@gmail.com>

> Create a copy or use a dto
> Le 18 oct. 2013 20:06, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
> > I only want to add some info to a retrieved object but i dont want to
> > persist that new info
> >
> >
> > 2013/10/18 José Luis Cetina <ma...@gmail.com>
> >
> > > But if use a transaction the new values will be committed and i want
> that
> > > values as a read only
> > >
> > >
> > > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> > >
> > >> Cause you arent managed...do the whole method in an ejb (with a
> > >> transaction)
> > >> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a
> > écrit
> > >> :
> > >>
> > >> > Ok, in the example i tried to set the list in the ejb, but if
> removed
> > >> and
> > >> > then put the list in the managed bean i get the same behavior
> > >> >
> > >> >
> > >> > @Named
> > >> > public class MyBean implements Serializable{
> > >> >
> > >> >  @EJB
> > >> >  private MyEJB ejb;
> > >> >
> > >> >  public void anyMethod(){
> > >> >    User user = ejb.getUserWithRoles();
> > >> >     //i will create a list of role just for try to set any role the
> > >> > userJohn
> > >> >       List<Role> roleList = new ArrayList<Role>(2);
> > >> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> > >> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
> role
> > 2
> > >> >
> > >> >      //setting the list of roles created to the user, as you can see
> > the
> > >> > list has 2 values but the value roles of userJohn always is set to
> > >> null, my
> > >> > setters and getters are correct
> > >> >      user.setRoles(roleList);
> > >> >
> > >> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
> > >> >  }
> > >> >
> > >> > }
> > >> >
> > >> >
> > >> >
> > >> >
> > >> > I dont want these roles get persisted i just want to modify my
> object,
> > >> is
> > >> > still the same even setting the value in the maged bean?
> > >> >
> > >> >
> > >> >
> > >> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> > >> >
> > >> > > Hi
> > >> > >
> > >> > > Your entity is not managed so you cant do it. It is normal.
> > >> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com>
> a
> > >> > écrit :
> > >> > >
> > >> > > > I sent this mail to openJPA mailing list but i dont know if this
> > >> could
> > >> > be
> > >> > > > something related to TomEE or just im doing something wrong.
> > >> > > >
> > >> > > >
> > >> > > > I have a "strange" behavior, with list properties of any kind of
> > >> > entity.
> > >> > > >
> > >> > > > When i try to set a non empty list to a retrieved entity that
> has
> > a
> > >> > list
> > >> > > > value with null (because is not fetched) the entity doesnt
> > "reflect"
> > >> > the
> > >> > > > new non-empty values that i set.
> > >> > > >
> > >> > > > Steps
> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> > >> > criterias.
> > >> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
> > >> default is
> > >> > > > lazy thats why if i dont do a fetch (i dont) the list is null
> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list
> > >> > > > 4. Here the list that i set has values but the user list has a
> > null
> > >> in
> > >> > > the
> > >> > > > roles list attribute even though i set it in the step 3
> > >> > > >
> > >> > > >
> > >> > > > I dont know why this is happening, the only way i could fix this
> > is
> > >> > > cloning
> > >> > > > (using SerializationUtils from Apache commons.lang3) the user
> > entity
> > >> > (see
> > >> > > > the steps) and with this work, but i dont know why.
> > >> > > >
> > >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> > >> > criterias.
> > >> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
> > >> > > > SerializationUtils.clone(originalRetrivedUser);
> > >> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
> > >> > clonedUser
> > >> > > > 4. The list of clonedUser has the correct values
> > >> > > >
> > >> > > >
> > >> > > > Why i need to clone? why i cannot set a list to the entity if is
> > not
> > >> > > > cloned?
> > >> > > >
> > >> > > >
> > >> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
> > >> > embedded)
> > >> > > >
> > >> > > >
> > >> > > > Scenario:
> > >> > > >
> > >> > > > ***** ENTITIES *****
> > >> > > > @Entity
> > >> > > > public class User implements Serializable{
> > >> > > >
> > >> > > >    @Id
> > >> > > >    private int id;
> > >> > > >    private String userName;
> > >> > > >    private String password;
> > >> > > >    @OneToMany(mappedBy = "user")
> > >> > > >    private List<Role> roles;
> > >> > > >
> > >> > > >    //getters and setters..
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > >
> > >> > > > @Entity
> > >> > > > public class Role implements Serializable{
> > >> > > >
> > >> > > >    @Id
> > >> > > >    private int id;
> > >> > > >    private String roleName;
> > >> > > >    @ManyToOne
> > >> > > >    @JoinColumn(name = "user_id")
> > >> > > >    private User user;
> > >> > > >
> > >> > > >   //getters and setters..
> > >> > > > }
> > >> > > >
> > >> > > >
> > >> > > > **** EJB CLASS ****
> > >> > > > @Stateless
> > >> > > > public class MyEJB{
> > >> > > >
> > >> > > >     @PersistenceContext(unitName ="ANY_NAME")
> > >> > > >     private EntityManager em;
> > >> > > >
> > >> > > >   public User getUserWithRoles(){
> > >> > > >
> > >> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
> > >> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> > >> > > >         Root<User> root = cq.from(User.class);
> > >> > > >
> > >> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
> > >> > > >
> > >> > > >        User userJohn = em.createQuery(cq).getSingleResult();  //
> > if
> > >> i
> > >> > > want
> > >> > > > this work i have to do User userJohn =
> > >> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());
> > >>  [1*]
> > >> > > >
> > >> > > >
> > >> > > >        //i will create a list of role just for try to set any
> role
> > >> the
> > >> > > > userJohn
> > >> > > >       List<Role> roleList = new ArrayList<Role>(2);
> > >> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding
> > role 1
> > >> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
> > >> role 2
> > >> > > >
> > >> > > >      //setting the list of roles created to the user, as you can
> > see
> > >> > the
> > >> > > > list has 2 values but the value roles of userJohn always is set
> to
> > >> > null,
> > >> > > my
> > >> > > > setters and getters are correct
> > >> > > >      userJohn.setRoles(roleList);
> > >> > > >
> > >> > > >      return userJohn;
> > >> > > >   }
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > > *** MANAGED BEAN ****
> > >> > > >
> > >> > > > @Named
> > >> > > > public class MyBean implements Serializable{
> > >> > > >
> > >> > > >  @EJB
> > >> > > >  private MyEJB ejb;
> > >> > > >
> > >> > > >  public void anyMethod(){
> > >> > > >    User user = ejb.getUserWithRoles();
> > >> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but
> if i
> > >> use
> > >> > > > clone in the ejb method (see 1*) i can get the corrected values.
> > >> > > >
> > >> > > >  }
> > >> > > >
> > >> > > >
> > >> > > > }
> > >> > > >
> > >> > > > I know i can get the values fetching but im trying to not do it
> > >> > > >
> > >> > >
> > >> >
> > >> >
> > >> >
> > >> > --
> > >> > -------------------------------------------------------------------
> > >> > *SCJA. José Luis Cetina*
> > >> > -------------------------------------------------------------------
> > >> >
> > >>
> > >
> > >
> > >
> > > --
> > > -------------------------------------------------------------------
> > > *SCJA. José Luis Cetina*
> > > -------------------------------------------------------------------
> > >
> >
> >
> >
> > --
> > -------------------------------------------------------------------
> > *SCJA. José Luis Cetina*
> > -------------------------------------------------------------------
> >
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Create a copy or use a dto
Le 18 oct. 2013 20:06, "José Luis Cetina" <ma...@gmail.com> a écrit :

> I only want to add some info to a retrieved object but i dont want to
> persist that new info
>
>
> 2013/10/18 José Luis Cetina <ma...@gmail.com>
>
> > But if use a transaction the new values will be committed and i want that
> > values as a read only
> >
> >
> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >
> >> Cause you arent managed...do the whole method in an ejb (with a
> >> transaction)
> >> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a
> écrit
> >> :
> >>
> >> > Ok, in the example i tried to set the list in the ejb, but if removed
> >> and
> >> > then put the list in the managed bean i get the same behavior
> >> >
> >> >
> >> > @Named
> >> > public class MyBean implements Serializable{
> >> >
> >> >  @EJB
> >> >  private MyEJB ejb;
> >> >
> >> >  public void anyMethod(){
> >> >    User user = ejb.getUserWithRoles();
> >> >     //i will create a list of role just for try to set any role the
> >> > userJohn
> >> >       List<Role> roleList = new ArrayList<Role>(2);
> >> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> >> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role
> 2
> >> >
> >> >      //setting the list of roles created to the user, as you can see
> the
> >> > list has 2 values but the value roles of userJohn always is set to
> >> null, my
> >> > setters and getters are correct
> >> >      user.setRoles(roleList);
> >> >
> >> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
> >> >  }
> >> >
> >> > }
> >> >
> >> >
> >> >
> >> >
> >> > I dont want these roles get persisted i just want to modify my object,
> >> is
> >> > still the same even setting the value in the maged bean?
> >> >
> >> >
> >> >
> >> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >> >
> >> > > Hi
> >> > >
> >> > > Your entity is not managed so you cant do it. It is normal.
> >> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a
> >> > écrit :
> >> > >
> >> > > > I sent this mail to openJPA mailing list but i dont know if this
> >> could
> >> > be
> >> > > > something related to TomEE or just im doing something wrong.
> >> > > >
> >> > > >
> >> > > > I have a "strange" behavior, with list properties of any kind of
> >> > entity.
> >> > > >
> >> > > > When i try to set a non empty list to a retrieved entity that has
> a
> >> > list
> >> > > > value with null (because is not fetched) the entity doesnt
> "reflect"
> >> > the
> >> > > > new non-empty values that i set.
> >> > > >
> >> > > > Steps
> >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> >> > criterias.
> >> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
> >> default is
> >> > > > lazy thats why if i dont do a fetch (i dont) the list is null
> >> > > > 3. Then try to fill the role list with a NON-EMPTY list
> >> > > > 4. Here the list that i set has values but the user list has a
> null
> >> in
> >> > > the
> >> > > > roles list attribute even though i set it in the step 3
> >> > > >
> >> > > >
> >> > > > I dont know why this is happening, the only way i could fix this
> is
> >> > > cloning
> >> > > > (using SerializationUtils from Apache commons.lang3) the user
> entity
> >> > (see
> >> > > > the steps) and with this work, but i dont know why.
> >> > > >
> >> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> >> > criterias.
> >> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
> >> > > > SerializationUtils.clone(originalRetrivedUser);
> >> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
> >> > clonedUser
> >> > > > 4. The list of clonedUser has the correct values
> >> > > >
> >> > > >
> >> > > > Why i need to clone? why i cannot set a list to the entity if is
> not
> >> > > > cloned?
> >> > > >
> >> > > >
> >> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
> >> > embedded)
> >> > > >
> >> > > >
> >> > > > Scenario:
> >> > > >
> >> > > > ***** ENTITIES *****
> >> > > > @Entity
> >> > > > public class User implements Serializable{
> >> > > >
> >> > > >    @Id
> >> > > >    private int id;
> >> > > >    private String userName;
> >> > > >    private String password;
> >> > > >    @OneToMany(mappedBy = "user")
> >> > > >    private List<Role> roles;
> >> > > >
> >> > > >    //getters and setters..
> >> > > >
> >> > > > }
> >> > > >
> >> > > >
> >> > > > @Entity
> >> > > > public class Role implements Serializable{
> >> > > >
> >> > > >    @Id
> >> > > >    private int id;
> >> > > >    private String roleName;
> >> > > >    @ManyToOne
> >> > > >    @JoinColumn(name = "user_id")
> >> > > >    private User user;
> >> > > >
> >> > > >   //getters and setters..
> >> > > > }
> >> > > >
> >> > > >
> >> > > > **** EJB CLASS ****
> >> > > > @Stateless
> >> > > > public class MyEJB{
> >> > > >
> >> > > >     @PersistenceContext(unitName ="ANY_NAME")
> >> > > >     private EntityManager em;
> >> > > >
> >> > > >   public User getUserWithRoles(){
> >> > > >
> >> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
> >> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> >> > > >         Root<User> root = cq.from(User.class);
> >> > > >
> >> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
> >> > > >
> >> > > >        User userJohn = em.createQuery(cq).getSingleResult();  //
> if
> >> i
> >> > > want
> >> > > > this work i have to do User userJohn =
> >> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());
> >>  [1*]
> >> > > >
> >> > > >
> >> > > >        //i will create a list of role just for try to set any role
> >> the
> >> > > > userJohn
> >> > > >       List<Role> roleList = new ArrayList<Role>(2);
> >> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding
> role 1
> >> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
> >> role 2
> >> > > >
> >> > > >      //setting the list of roles created to the user, as you can
> see
> >> > the
> >> > > > list has 2 values but the value roles of userJohn always is set to
> >> > null,
> >> > > my
> >> > > > setters and getters are correct
> >> > > >      userJohn.setRoles(roleList);
> >> > > >
> >> > > >      return userJohn;
> >> > > >   }
> >> > > >
> >> > > > }
> >> > > >
> >> > > > *** MANAGED BEAN ****
> >> > > >
> >> > > > @Named
> >> > > > public class MyBean implements Serializable{
> >> > > >
> >> > > >  @EJB
> >> > > >  private MyEJB ejb;
> >> > > >
> >> > > >  public void anyMethod(){
> >> > > >    User user = ejb.getUserWithRoles();
> >> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i
> >> use
> >> > > > clone in the ejb method (see 1*) i can get the corrected values.
> >> > > >
> >> > > >  }
> >> > > >
> >> > > >
> >> > > > }
> >> > > >
> >> > > > I know i can get the values fetching but im trying to not do it
> >> > > >
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > -------------------------------------------------------------------
> >> > *SCJA. José Luis Cetina*
> >> > -------------------------------------------------------------------
> >> >
> >>
> >
> >
> >
> > --
> > -------------------------------------------------------------------
> > *SCJA. José Luis Cetina*
> > -------------------------------------------------------------------
> >
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
I only want to add some info to a retrieved object but i dont want to
persist that new info


2013/10/18 José Luis Cetina <ma...@gmail.com>

> But if use a transaction the new values will be committed and i want that
> values as a read only
>
>
> 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>
>> Cause you arent managed...do the whole method in an ejb (with a
>> transaction)
>> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a écrit
>> :
>>
>> > Ok, in the example i tried to set the list in the ejb, but if removed
>> and
>> > then put the list in the managed bean i get the same behavior
>> >
>> >
>> > @Named
>> > public class MyBean implements Serializable{
>> >
>> >  @EJB
>> >  private MyEJB ejb;
>> >
>> >  public void anyMethod(){
>> >    User user = ejb.getUserWithRoles();
>> >     //i will create a list of role just for try to set any role the
>> > userJohn
>> >       List<Role> roleList = new ArrayList<Role>(2);
>> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
>> >
>> >      //setting the list of roles created to the user, as you can see the
>> > list has 2 values but the value roles of userJohn always is set to
>> null, my
>> > setters and getters are correct
>> >      user.setRoles(roleList);
>> >
>> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
>> >  }
>> >
>> > }
>> >
>> >
>> >
>> >
>> > I dont want these roles get persisted i just want to modify my object,
>> is
>> > still the same even setting the value in the maged bean?
>> >
>> >
>> >
>> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>> >
>> > > Hi
>> > >
>> > > Your entity is not managed so you cant do it. It is normal.
>> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a
>> > écrit :
>> > >
>> > > > I sent this mail to openJPA mailing list but i dont know if this
>> could
>> > be
>> > > > something related to TomEE or just im doing something wrong.
>> > > >
>> > > >
>> > > > I have a "strange" behavior, with list properties of any kind of
>> > entity.
>> > > >
>> > > > When i try to set a non empty list to a retrieved entity that has a
>> > list
>> > > > value with null (because is not fetched) the entity doesnt "reflect"
>> > the
>> > > > new non-empty values that i set.
>> > > >
>> > > > Steps
>> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> > criterias.
>> > > > 2. The retrieved entity has a OneToMany attribute (roles) by
>> default is
>> > > > lazy thats why if i dont do a fetch (i dont) the list is null
>> > > > 3. Then try to fill the role list with a NON-EMPTY list
>> > > > 4. Here the list that i set has values but the user list has a null
>> in
>> > > the
>> > > > roles list attribute even though i set it in the step 3
>> > > >
>> > > >
>> > > > I dont know why this is happening, the only way i could fix this is
>> > > cloning
>> > > > (using SerializationUtils from Apache commons.lang3) the user entity
>> > (see
>> > > > the steps) and with this work, but i dont know why.
>> > > >
>> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
>> > criterias.
>> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
>> > > > SerializationUtils.clone(originalRetrivedUser);
>> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
>> > clonedUser
>> > > > 4. The list of clonedUser has the correct values
>> > > >
>> > > >
>> > > > Why i need to clone? why i cannot set a list to the entity if is not
>> > > > cloned?
>> > > >
>> > > >
>> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
>> > embedded)
>> > > >
>> > > >
>> > > > Scenario:
>> > > >
>> > > > ***** ENTITIES *****
>> > > > @Entity
>> > > > public class User implements Serializable{
>> > > >
>> > > >    @Id
>> > > >    private int id;
>> > > >    private String userName;
>> > > >    private String password;
>> > > >    @OneToMany(mappedBy = "user")
>> > > >    private List<Role> roles;
>> > > >
>> > > >    //getters and setters..
>> > > >
>> > > > }
>> > > >
>> > > >
>> > > > @Entity
>> > > > public class Role implements Serializable{
>> > > >
>> > > >    @Id
>> > > >    private int id;
>> > > >    private String roleName;
>> > > >    @ManyToOne
>> > > >    @JoinColumn(name = "user_id")
>> > > >    private User user;
>> > > >
>> > > >   //getters and setters..
>> > > > }
>> > > >
>> > > >
>> > > > **** EJB CLASS ****
>> > > > @Stateless
>> > > > public class MyEJB{
>> > > >
>> > > >     @PersistenceContext(unitName ="ANY_NAME")
>> > > >     private EntityManager em;
>> > > >
>> > > >   public User getUserWithRoles(){
>> > > >
>> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
>> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
>> > > >         Root<User> root = cq.from(User.class);
>> > > >
>> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
>> > > >
>> > > >        User userJohn = em.createQuery(cq).getSingleResult();  // if
>> i
>> > > want
>> > > > this work i have to do User userJohn =
>> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());
>>  [1*]
>> > > >
>> > > >
>> > > >        //i will create a list of role just for try to set any role
>> the
>> > > > userJohn
>> > > >       List<Role> roleList = new ArrayList<Role>(2);
>> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
>> role 2
>> > > >
>> > > >      //setting the list of roles created to the user, as you can see
>> > the
>> > > > list has 2 values but the value roles of userJohn always is set to
>> > null,
>> > > my
>> > > > setters and getters are correct
>> > > >      userJohn.setRoles(roleList);
>> > > >
>> > > >      return userJohn;
>> > > >   }
>> > > >
>> > > > }
>> > > >
>> > > > *** MANAGED BEAN ****
>> > > >
>> > > > @Named
>> > > > public class MyBean implements Serializable{
>> > > >
>> > > >  @EJB
>> > > >  private MyEJB ejb;
>> > > >
>> > > >  public void anyMethod(){
>> > > >    User user = ejb.getUserWithRoles();
>> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i
>> use
>> > > > clone in the ejb method (see 1*) i can get the corrected values.
>> > > >
>> > > >  }
>> > > >
>> > > >
>> > > > }
>> > > >
>> > > > I know i can get the values fetching but im trying to not do it
>> > > >
>> > >
>> >
>> >
>> >
>> > --
>> > -------------------------------------------------------------------
>> > *SCJA. José Luis Cetina*
>> > -------------------------------------------------------------------
>> >
>>
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
But if use a transaction the new values will be committed and i want that
values as a read only


2013/10/18 Romain Manni-Bucau <rm...@gmail.com>

> Cause you arent managed...do the whole method in an ejb (with a
> transaction)
> Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
> > Ok, in the example i tried to set the list in the ejb, but if removed and
> > then put the list in the managed bean i get the same behavior
> >
> >
> > @Named
> > public class MyBean implements Serializable{
> >
> >  @EJB
> >  private MyEJB ejb;
> >
> >  public void anyMethod(){
> >    User user = ejb.getUserWithRoles();
> >     //i will create a list of role just for try to set any role the
> > userJohn
> >       List<Role> roleList = new ArrayList<Role>(2);
> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
> >
> >      //setting the list of roles created to the user, as you can see the
> > list has 2 values but the value roles of userJohn always is set to null,
> my
> > setters and getters are correct
> >      user.setRoles(roleList);
> >
> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
> >  }
> >
> > }
> >
> >
> >
> >
> > I dont want these roles get persisted i just want to modify my object, is
> > still the same even setting the value in the maged bean?
> >
> >
> >
> > 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
> >
> > > Hi
> > >
> > > Your entity is not managed so you cant do it. It is normal.
> > > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a
> > écrit :
> > >
> > > > I sent this mail to openJPA mailing list but i dont know if this
> could
> > be
> > > > something related to TomEE or just im doing something wrong.
> > > >
> > > >
> > > > I have a "strange" behavior, with list properties of any kind of
> > entity.
> > > >
> > > > When i try to set a non empty list to a retrieved entity that has a
> > list
> > > > value with null (because is not fetched) the entity doesnt "reflect"
> > the
> > > > new non-empty values that i set.
> > > >
> > > > Steps
> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> > criterias.
> > > > 2. The retrieved entity has a OneToMany attribute (roles) by default
> is
> > > > lazy thats why if i dont do a fetch (i dont) the list is null
> > > > 3. Then try to fill the role list with a NON-EMPTY list
> > > > 4. Here the list that i set has values but the user list has a null
> in
> > > the
> > > > roles list attribute even though i set it in the step 3
> > > >
> > > >
> > > > I dont know why this is happening, the only way i could fix this is
> > > cloning
> > > > (using SerializationUtils from Apache commons.lang3) the user entity
> > (see
> > > > the steps) and with this work, but i dont know why.
> > > >
> > > > 1. Retrieve any entity (ex. user) from database in an ejb with
> > criterias.
> > > > 2. Clone retrieved entity and asig to new one User clonedUser =
> > > > SerializationUtils.clone(originalRetrivedUser);
> > > > 3. Then try to fill the role list with a NON-EMPTY list to the
> > clonedUser
> > > > 4. The list of clonedUser has the correct values
> > > >
> > > >
> > > > Why i need to clone? why i cannot set a list to the entity if is not
> > > > cloned?
> > > >
> > > >
> > > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
> > embedded)
> > > >
> > > >
> > > > Scenario:
> > > >
> > > > ***** ENTITIES *****
> > > > @Entity
> > > > public class User implements Serializable{
> > > >
> > > >    @Id
> > > >    private int id;
> > > >    private String userName;
> > > >    private String password;
> > > >    @OneToMany(mappedBy = "user")
> > > >    private List<Role> roles;
> > > >
> > > >    //getters and setters..
> > > >
> > > > }
> > > >
> > > >
> > > > @Entity
> > > > public class Role implements Serializable{
> > > >
> > > >    @Id
> > > >    private int id;
> > > >    private String roleName;
> > > >    @ManyToOne
> > > >    @JoinColumn(name = "user_id")
> > > >    private User user;
> > > >
> > > >   //getters and setters..
> > > > }
> > > >
> > > >
> > > > **** EJB CLASS ****
> > > > @Stateless
> > > > public class MyEJB{
> > > >
> > > >     @PersistenceContext(unitName ="ANY_NAME")
> > > >     private EntityManager em;
> > > >
> > > >   public User getUserWithRoles(){
> > > >
> > > >         CriteriaBuilder cb = em.getCriteriaBuilder();
> > > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> > > >         Root<User> root = cq.from(User.class);
> > > >
> > > >         cq.where(cb.equal(root.get(User_.userName),"john"));
> > > >
> > > >        User userJohn = em.createQuery(cq).getSingleResult();  // if i
> > > want
> > > > this work i have to do User userJohn =
> > > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]
> > > >
> > > >
> > > >        //i will create a list of role just for try to set any role
> the
> > > > userJohn
> > > >       List<Role> roleList = new ArrayList<Role>(2);
> > > >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> > > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding
> role 2
> > > >
> > > >      //setting the list of roles created to the user, as you can see
> > the
> > > > list has 2 values but the value roles of userJohn always is set to
> > null,
> > > my
> > > > setters and getters are correct
> > > >      userJohn.setRoles(roleList);
> > > >
> > > >      return userJohn;
> > > >   }
> > > >
> > > > }
> > > >
> > > > *** MANAGED BEAN ****
> > > >
> > > > @Named
> > > > public class MyBean implements Serializable{
> > > >
> > > >  @EJB
> > > >  private MyEJB ejb;
> > > >
> > > >  public void anyMethod(){
> > > >    User user = ejb.getUserWithRoles();
> > > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i
> use
> > > > clone in the ejb method (see 1*) i can get the corrected values.
> > > >
> > > >  }
> > > >
> > > >
> > > > }
> > > >
> > > > I know i can get the values fetching but im trying to not do it
> > > >
> > >
> >
> >
> >
> > --
> > -------------------------------------------------------------------
> > *SCJA. José Luis Cetina*
> > -------------------------------------------------------------------
> >
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Cause you arent managed...do the whole method in an ejb (with a transaction)
Le 18 oct. 2013 19:30, "José Luis Cetina" <ma...@gmail.com> a écrit :

> Ok, in the example i tried to set the list in the ejb, but if removed and
> then put the list in the managed bean i get the same behavior
>
>
> @Named
> public class MyBean implements Serializable{
>
>  @EJB
>  private MyEJB ejb;
>
>  public void anyMethod(){
>    User user = ejb.getUserWithRoles();
>     //i will create a list of role just for try to set any role the
> userJohn
>       List<Role> roleList = new ArrayList<Role>(2);
>       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
>
>      //setting the list of roles created to the user, as you can see the
> list has 2 values but the value roles of userJohn always is set to null, my
> setters and getters are correct
>      user.setRoles(roleList);
>
>     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
>  }
>
> }
>
>
>
>
> I dont want these roles get persisted i just want to modify my object, is
> still the same even setting the value in the maged bean?
>
>
>
> 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>
> > Hi
> >
> > Your entity is not managed so you cant do it. It is normal.
> > Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a
> écrit :
> >
> > > I sent this mail to openJPA mailing list but i dont know if this could
> be
> > > something related to TomEE or just im doing something wrong.
> > >
> > >
> > > I have a "strange" behavior, with list properties of any kind of
> entity.
> > >
> > > When i try to set a non empty list to a retrieved entity that has a
> list
> > > value with null (because is not fetched) the entity doesnt "reflect"
> the
> > > new non-empty values that i set.
> > >
> > > Steps
> > > 1. Retrieve any entity (ex. user) from database in an ejb with
> criterias.
> > > 2. The retrieved entity has a OneToMany attribute (roles) by default is
> > > lazy thats why if i dont do a fetch (i dont) the list is null
> > > 3. Then try to fill the role list with a NON-EMPTY list
> > > 4. Here the list that i set has values but the user list has a null in
> > the
> > > roles list attribute even though i set it in the step 3
> > >
> > >
> > > I dont know why this is happening, the only way i could fix this is
> > cloning
> > > (using SerializationUtils from Apache commons.lang3) the user entity
> (see
> > > the steps) and with this work, but i dont know why.
> > >
> > > 1. Retrieve any entity (ex. user) from database in an ejb with
> criterias.
> > > 2. Clone retrieved entity and asig to new one User clonedUser =
> > > SerializationUtils.clone(originalRetrivedUser);
> > > 3. Then try to fill the role list with a NON-EMPTY list to the
> clonedUser
> > > 4. The list of clonedUser has the correct values
> > >
> > >
> > > Why i need to clone? why i cannot set a list to the entity if is not
> > > cloned?
> > >
> > >
> > > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version
> embedded)
> > >
> > >
> > > Scenario:
> > >
> > > ***** ENTITIES *****
> > > @Entity
> > > public class User implements Serializable{
> > >
> > >    @Id
> > >    private int id;
> > >    private String userName;
> > >    private String password;
> > >    @OneToMany(mappedBy = "user")
> > >    private List<Role> roles;
> > >
> > >    //getters and setters..
> > >
> > > }
> > >
> > >
> > > @Entity
> > > public class Role implements Serializable{
> > >
> > >    @Id
> > >    private int id;
> > >    private String roleName;
> > >    @ManyToOne
> > >    @JoinColumn(name = "user_id")
> > >    private User user;
> > >
> > >   //getters and setters..
> > > }
> > >
> > >
> > > **** EJB CLASS ****
> > > @Stateless
> > > public class MyEJB{
> > >
> > >     @PersistenceContext(unitName ="ANY_NAME")
> > >     private EntityManager em;
> > >
> > >   public User getUserWithRoles(){
> > >
> > >         CriteriaBuilder cb = em.getCriteriaBuilder();
> > >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> > >         Root<User> root = cq.from(User.class);
> > >
> > >         cq.where(cb.equal(root.get(User_.userName),"john"));
> > >
> > >        User userJohn = em.createQuery(cq).getSingleResult();  // if i
> > want
> > > this work i have to do User userJohn =
> > > SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]
> > >
> > >
> > >        //i will create a list of role just for try to set any role the
> > > userJohn
> > >       List<Role> roleList = new ArrayList<Role>(2);
> > >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> > >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
> > >
> > >      //setting the list of roles created to the user, as you can see
> the
> > > list has 2 values but the value roles of userJohn always is set to
> null,
> > my
> > > setters and getters are correct
> > >      userJohn.setRoles(roleList);
> > >
> > >      return userJohn;
> > >   }
> > >
> > > }
> > >
> > > *** MANAGED BEAN ****
> > >
> > > @Named
> > > public class MyBean implements Serializable{
> > >
> > >  @EJB
> > >  private MyEJB ejb;
> > >
> > >  public void anyMethod(){
> > >    User user = ejb.getUserWithRoles();
> > >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i use
> > > clone in the ejb method (see 1*) i can get the corrected values.
> > >
> > >  }
> > >
> > >
> > > }
> > >
> > > I know i can get the values fetching but im trying to not do it
> > >
> >
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Do you have any advice?


2013/10/18 José Luis Cetina <ma...@gmail.com>

> Ok, in the example i tried to set the list in the ejb, but if removed and
> then put the list in the managed bean i get the same behavior
>
>
> @Named
> public class MyBean implements Serializable{
>
>  @EJB
>  private MyEJB ejb;
>
>  public void anyMethod(){
>    User user = ejb.getUserWithRoles();
>     //i will create a list of role just for try to set any role the
> userJohn
>       List<Role> roleList = new ArrayList<Role>(2);
>       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
>
>      //setting the list of roles created to the user, as you can see the
> list has 2 values but the value roles of userJohn always is set to null, my
> setters and getters are correct
>      user.setRoles(roleList);
>
>     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
>  }
>
> }
>
>
>
>
> I dont want these roles get persisted i just want to modify my object, is
> still the same even setting the value in the maged bean?
>
>
>
> 2013/10/18 Romain Manni-Bucau <rm...@gmail.com>
>
>> Hi
>>
>> Your entity is not managed so you cant do it. It is normal.
>> Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a écrit
>> :
>>
>> > I sent this mail to openJPA mailing list but i dont know if this could
>> be
>> > something related to TomEE or just im doing something wrong.
>> >
>> >
>> > I have a "strange" behavior, with list properties of any kind of entity.
>> >
>> > When i try to set a non empty list to a retrieved entity that has a list
>> > value with null (because is not fetched) the entity doesnt "reflect" the
>> > new non-empty values that i set.
>> >
>> > Steps
>> > 1. Retrieve any entity (ex. user) from database in an ejb with
>> criterias.
>> > 2. The retrieved entity has a OneToMany attribute (roles) by default is
>> > lazy thats why if i dont do a fetch (i dont) the list is null
>> > 3. Then try to fill the role list with a NON-EMPTY list
>> > 4. Here the list that i set has values but the user list has a null in
>> the
>> > roles list attribute even though i set it in the step 3
>> >
>> >
>> > I dont know why this is happening, the only way i could fix this is
>> cloning
>> > (using SerializationUtils from Apache commons.lang3) the user entity
>> (see
>> > the steps) and with this work, but i dont know why.
>> >
>> > 1. Retrieve any entity (ex. user) from database in an ejb with
>> criterias.
>> > 2. Clone retrieved entity and asig to new one User clonedUser =
>> > SerializationUtils.clone(originalRetrivedUser);
>> > 3. Then try to fill the role list with a NON-EMPTY list to the
>> clonedUser
>> > 4. The list of clonedUser has the correct values
>> >
>> >
>> > Why i need to clone? why i cannot set a list to the entity if is not
>> > cloned?
>> >
>> >
>> > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version embedded)
>> >
>> >
>> > Scenario:
>> >
>> > ***** ENTITIES *****
>> > @Entity
>> > public class User implements Serializable{
>> >
>> >    @Id
>> >    private int id;
>> >    private String userName;
>> >    private String password;
>> >    @OneToMany(mappedBy = "user")
>> >    private List<Role> roles;
>> >
>> >    //getters and setters..
>> >
>> > }
>> >
>> >
>> > @Entity
>> > public class Role implements Serializable{
>> >
>> >    @Id
>> >    private int id;
>> >    private String roleName;
>> >    @ManyToOne
>> >    @JoinColumn(name = "user_id")
>> >    private User user;
>> >
>> >   //getters and setters..
>> > }
>> >
>> >
>> > **** EJB CLASS ****
>> > @Stateless
>> > public class MyEJB{
>> >
>> >     @PersistenceContext(unitName ="ANY_NAME")
>> >     private EntityManager em;
>> >
>> >   public User getUserWithRoles(){
>> >
>> >         CriteriaBuilder cb = em.getCriteriaBuilder();
>> >         CriteriaQuery<User> cq = cb.createQuery(User.class);
>> >         Root<User> root = cq.from(User.class);
>> >
>> >         cq.where(cb.equal(root.get(User_.userName),"john"));
>> >
>> >        User userJohn = em.createQuery(cq).getSingleResult();  // if i
>> want
>> > this work i have to do User userJohn =
>> > SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]
>> >
>> >
>> >        //i will create a list of role just for try to set any role the
>> > userJohn
>> >       List<Role> roleList = new ArrayList<Role>(2);
>> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
>> >
>> >      //setting the list of roles created to the user, as you can see the
>> > list has 2 values but the value roles of userJohn always is set to
>> null, my
>> > setters and getters are correct
>> >      userJohn.setRoles(roleList);
>> >
>> >      return userJohn;
>> >   }
>> >
>> > }
>> >
>> > *** MANAGED BEAN ****
>> >
>> > @Named
>> > public class MyBean implements Serializable{
>> >
>> >  @EJB
>> >  private MyEJB ejb;
>> >
>> >  public void anyMethod(){
>> >    User user = ejb.getUserWithRoles();
>> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i use
>> > clone in the ejb method (see 1*) i can get the corrected values.
>> >
>> >  }
>> >
>> >
>> > }
>> >
>> > I know i can get the values fetching but im trying to not do it
>> >
>>
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by José Luis Cetina <ma...@gmail.com>.
Ok, in the example i tried to set the list in the ejb, but if removed and
then put the list in the managed bean i get the same behavior


@Named
public class MyBean implements Serializable{

 @EJB
 private MyEJB ejb;

 public void anyMethod(){
   User user = ejb.getUserWithRoles();
    //i will create a list of role just for try to set any role the userJohn
      List<Role> roleList = new ArrayList<Role>(2);
      roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
      roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2

     //setting the list of roles created to the user, as you can see the
list has 2 values but the value roles of userJohn always is set to null, my
setters and getters are correct
     user.setRoles(roleList);

    user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL
 }

}




I dont want these roles get persisted i just want to modify my object, is
still the same even setting the value in the maged bean?



2013/10/18 Romain Manni-Bucau <rm...@gmail.com>

> Hi
>
> Your entity is not managed so you cant do it. It is normal.
> Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a écrit :
>
> > I sent this mail to openJPA mailing list but i dont know if this could be
> > something related to TomEE or just im doing something wrong.
> >
> >
> > I have a "strange" behavior, with list properties of any kind of entity.
> >
> > When i try to set a non empty list to a retrieved entity that has a list
> > value with null (because is not fetched) the entity doesnt "reflect" the
> > new non-empty values that i set.
> >
> > Steps
> > 1. Retrieve any entity (ex. user) from database in an ejb with criterias.
> > 2. The retrieved entity has a OneToMany attribute (roles) by default is
> > lazy thats why if i dont do a fetch (i dont) the list is null
> > 3. Then try to fill the role list with a NON-EMPTY list
> > 4. Here the list that i set has values but the user list has a null in
> the
> > roles list attribute even though i set it in the step 3
> >
> >
> > I dont know why this is happening, the only way i could fix this is
> cloning
> > (using SerializationUtils from Apache commons.lang3) the user entity (see
> > the steps) and with this work, but i dont know why.
> >
> > 1. Retrieve any entity (ex. user) from database in an ejb with criterias.
> > 2. Clone retrieved entity and asig to new one User clonedUser =
> > SerializationUtils.clone(originalRetrivedUser);
> > 3. Then try to fill the role list with a NON-EMPTY list to the clonedUser
> > 4. The list of clonedUser has the correct values
> >
> >
> > Why i need to clone? why i cannot set a list to the entity if is not
> > cloned?
> >
> >
> > Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version embedded)
> >
> >
> > Scenario:
> >
> > ***** ENTITIES *****
> > @Entity
> > public class User implements Serializable{
> >
> >    @Id
> >    private int id;
> >    private String userName;
> >    private String password;
> >    @OneToMany(mappedBy = "user")
> >    private List<Role> roles;
> >
> >    //getters and setters..
> >
> > }
> >
> >
> > @Entity
> > public class Role implements Serializable{
> >
> >    @Id
> >    private int id;
> >    private String roleName;
> >    @ManyToOne
> >    @JoinColumn(name = "user_id")
> >    private User user;
> >
> >   //getters and setters..
> > }
> >
> >
> > **** EJB CLASS ****
> > @Stateless
> > public class MyEJB{
> >
> >     @PersistenceContext(unitName ="ANY_NAME")
> >     private EntityManager em;
> >
> >   public User getUserWithRoles(){
> >
> >         CriteriaBuilder cb = em.getCriteriaBuilder();
> >         CriteriaQuery<User> cq = cb.createQuery(User.class);
> >         Root<User> root = cq.from(User.class);
> >
> >         cq.where(cb.equal(root.get(User_.userName),"john"));
> >
> >        User userJohn = em.createQuery(cq).getSingleResult();  // if i
> want
> > this work i have to do User userJohn =
> > SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]
> >
> >
> >        //i will create a list of role just for try to set any role the
> > userJohn
> >       List<Role> roleList = new ArrayList<Role>(2);
> >       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
> >       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
> >
> >      //setting the list of roles created to the user, as you can see the
> > list has 2 values but the value roles of userJohn always is set to null,
> my
> > setters and getters are correct
> >      userJohn.setRoles(roleList);
> >
> >      return userJohn;
> >   }
> >
> > }
> >
> > *** MANAGED BEAN ****
> >
> > @Named
> > public class MyBean implements Serializable{
> >
> >  @EJB
> >  private MyEJB ejb;
> >
> >  public void anyMethod(){
> >    User user = ejb.getUserWithRoles();
> >     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i use
> > clone in the ejb method (see 1*) i can get the corrected values.
> >
> >  }
> >
> >
> > }
> >
> > I know i can get the values fetching but im trying to not do it
> >
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Entity cant be refreshed with new list values

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

Your entity is not managed so you cant do it. It is normal.
Le 18 oct. 2013 19:09, "José Luis Cetina" <ma...@gmail.com> a écrit :

> I sent this mail to openJPA mailing list but i dont know if this could be
> something related to TomEE or just im doing something wrong.
>
>
> I have a "strange" behavior, with list properties of any kind of entity.
>
> When i try to set a non empty list to a retrieved entity that has a list
> value with null (because is not fetched) the entity doesnt "reflect" the
> new non-empty values that i set.
>
> Steps
> 1. Retrieve any entity (ex. user) from database in an ejb with criterias.
> 2. The retrieved entity has a OneToMany attribute (roles) by default is
> lazy thats why if i dont do a fetch (i dont) the list is null
> 3. Then try to fill the role list with a NON-EMPTY list
> 4. Here the list that i set has values but the user list has a null in the
> roles list attribute even though i set it in the step 3
>
>
> I dont know why this is happening, the only way i could fix this is cloning
> (using SerializationUtils from Apache commons.lang3) the user entity (see
> the steps) and with this work, but i dont know why.
>
> 1. Retrieve any entity (ex. user) from database in an ejb with criterias.
> 2. Clone retrieved entity and asig to new one User clonedUser =
> SerializationUtils.clone(originalRetrivedUser);
> 3. Then try to fill the role list with a NON-EMPTY list to the clonedUser
> 4. The list of clonedUser has the correct values
>
>
> Why i need to clone? why i cannot set a list to the entity if is not
> cloned?
>
>
> Im using Apache TomEE 1.6.0-SNAPSHOT (with the openjpa version embedded)
>
>
> Scenario:
>
> ***** ENTITIES *****
> @Entity
> public class User implements Serializable{
>
>    @Id
>    private int id;
>    private String userName;
>    private String password;
>    @OneToMany(mappedBy = "user")
>    private List<Role> roles;
>
>    //getters and setters..
>
> }
>
>
> @Entity
> public class Role implements Serializable{
>
>    @Id
>    private int id;
>    private String roleName;
>    @ManyToOne
>    @JoinColumn(name = "user_id")
>    private User user;
>
>   //getters and setters..
> }
>
>
> **** EJB CLASS ****
> @Stateless
> public class MyEJB{
>
>     @PersistenceContext(unitName ="ANY_NAME")
>     private EntityManager em;
>
>   public User getUserWithRoles(){
>
>         CriteriaBuilder cb = em.getCriteriaBuilder();
>         CriteriaQuery<User> cq = cb.createQuery(User.class);
>         Root<User> root = cq.from(User.class);
>
>         cq.where(cb.equal(root.get(User_.userName),"john"));
>
>        User userJohn = em.createQuery(cq).getSingleResult();  // if i want
> this work i have to do User userJohn =
> SerializationUtils.clone(em.createQuery(cq).getSingleResult());  [1*]
>
>
>        //i will create a list of role just for try to set any role the
> userJohn
>       List<Role> roleList = new ArrayList<Role>(2);
>       roleList.add(new Role(1,'ADMIN'); //creating and adding role 1
>       roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2
>
>      //setting the list of roles created to the user, as you can see the
> list has 2 values but the value roles of userJohn always is set to null, my
> setters and getters are correct
>      userJohn.setRoles(roleList);
>
>      return userJohn;
>   }
>
> }
>
> *** MANAGED BEAN ****
>
> @Named
> public class MyBean implements Serializable{
>
>  @EJB
>  private MyEJB ejb;
>
>  public void anyMethod(){
>    User user = ejb.getUserWithRoles();
>     user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL but if i use
> clone in the ejb method (see 1*) i can get the corrected values.
>
>  }
>
>
> }
>
> I know i can get the values fetching but im trying to not do it
>