Return-Path: X-Original-To: apmail-openjpa-users-archive@minotaur.apache.org Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BE5D210A0F for ; Fri, 18 Oct 2013 20:03:16 +0000 (UTC) Received: (qmail 77456 invoked by uid 500); 18 Oct 2013 20:03:12 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 77420 invoked by uid 500); 18 Oct 2013 20:03:07 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 77406 invoked by uid 99); 18 Oct 2013 20:03:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 20:03:05 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of maxtorzito@gmail.com designates 209.85.217.176 as permitted sender) Received: from [209.85.217.176] (HELO mail-lb0-f176.google.com) (209.85.217.176) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 20:03:00 +0000 Received: by mail-lb0-f176.google.com with SMTP id y6so3485120lbh.21 for ; Fri, 18 Oct 2013 13:02:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=AotJLG/9cish39NDdYhYlOkjlt8xr37Y9dUmq7XOrIA=; b=Klo/MuuCn4sA45CW7irCov0wRDszrtlcqJwsLBGqQrX4+b9+oEDIfp4MHmyyw6djBA 5v1bwd1Fs0f2qoD08kEQJ7V4/IMQ8bTCNCehmgQmfNbu59iPNwsTTx3xzuDdYf4DN+pw nHnJ3HNNxLCu+YDwT6n88JBkQGjCu7MCA3E4atodRgXfMISS6PVYSzcNaxpZXWHrdcdR 34Kot8hP6xVYm2Ja6Ylg7OJofSnOC+QdWXlPXPVP9giTC+UqwmBu0yiP0R/qb1lhJeUG 9YL/eAEt7dTl6Ej3pLt1rbqFhTcCRmF6m04x0/Tcsc2fT8pr8L8FSoCgOvInghTSNTHY u2JA== MIME-Version: 1.0 X-Received: by 10.112.198.39 with SMTP id iz7mr3826862lbc.24.1382126559137; Fri, 18 Oct 2013 13:02:39 -0700 (PDT) Received: by 10.112.5.164 with HTTP; Fri, 18 Oct 2013 13:02:39 -0700 (PDT) In-Reply-To: References: Date: Fri, 18 Oct 2013 15:02:39 -0500 Message-ID: Subject: Re: Entity cant be refreshed with new list values From: =?ISO-8859-1?Q?Jos=E9_Luis_Cetina?= To: users@openjpa.apache.org Content-Type: multipart/alternative; boundary=001a11c233ea97979a04e9096a94 X-Virus-Checked: Checked by ClamAV on apache.org --001a11c233ea97979a04e9096a94 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable In the previous 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 =3D ejb.getUserWithRoles(); //i will create a list of role just for try to set any role the userJoh= n List roleList =3D new ArrayList(2); roleList.add(new Role(1,'ADMIN'); //creating and adding role 1 roleList.add(new Role(2,'DEVELOPER');//creating and adding role 2 user.setRoles(roleList); user.getRoles(); //<---- HERE THE LIST IS ALWAYS NULL } } 2013/10/18 Jos=E9 Luis Cetina > > 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 th= e > 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 =3D > 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 =3D "user") > private List roles; > > //getters and setters.. > > } > > > @Entity > public class Role implements Serializable{ > > @Id > private int id; > private String roleName; > @ManyToOne > @JoinColumn(name =3D "user_id") > private User user; > > //getters and setters.. > } > > > **** EJB CLASS **** > @Stateless > public class MyEJB{ > > @PersistenceContext(unitName =3D"ANY_NAME") > private EntityManager em; > > public User getUserWithRoles(){ > > CriteriaBuilder cb =3D em.getCriteriaBuilder(); > CriteriaQuery cq =3D cb.createQuery(User.class); > Root root =3D cq.from(User.class); > > cq.where(cb.equal(root.get(User_.userName),"john")); > > User userJohn =3D em.createQuery(cq).getSingleResult(); // if i w= ant > this work i have to do User userJohn =3D > SerializationUtils.clone(em.createQuery(cq).getSingleResult()); [1*] > > > //i will create a list of role just for try to set any role the > userJohn > List roleList =3D new ArrayList(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 =3D 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 > --=20 ------------------------------------------------------------------- *SCJA. Jos=E9 Luis Cetina* ------------------------------------------------------------------- --001a11c233ea97979a04e9096a94--