Return-Path: X-Original-To: apmail-tomee-users-archive@www.apache.org Delivered-To: apmail-tomee-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E847910727 for ; Fri, 18 Oct 2013 18:15:43 +0000 (UTC) Received: (qmail 53893 invoked by uid 500); 18 Oct 2013 18:15:43 -0000 Delivered-To: apmail-tomee-users-archive@tomee.apache.org Received: (qmail 53307 invoked by uid 500); 18 Oct 2013 18:15:41 -0000 Mailing-List: contact users-help@tomee.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@tomee.apache.org Delivered-To: mailing list users@tomee.apache.org Received: (qmail 53298 invoked by uid 99); 18 Oct 2013 18:15:40 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 18:15:40 +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 (nike.apache.org: domain of rmannibucau@gmail.com designates 209.85.128.52 as permitted sender) Received: from [209.85.128.52] (HELO mail-qe0-f52.google.com) (209.85.128.52) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 18:15:32 +0000 Received: by mail-qe0-f52.google.com with SMTP id w7so2128713qeb.11 for ; Fri, 18 Oct 2013 11:15:12 -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=4xctCxm2G4EoipeLKrabeoLAnHgN9SsHEGr8NVgr6eQ=; b=tWmKA9TKccuHvyNgYcDPhTK8whxBpb+wlXAsGMeFj2amypzSMU3Zx5pWrfznsWBrHl RI1X/L2vXz1d/W8XpClUOazdc1ukJhtO77K9g91TKG5sYff0tTYroZgwxBHeUkv6uldZ oTqU6bXpHLeBokiO0zO930QEqWKuHZXecVKVgHWMYIG3UYY0MfJXnYErYDCw30LPF80M sHhGoxh8zL+WWC05XIIdu2inT1aIZtIwPobcO2W6QDqBvmyHquYy0pP42CxDsY+gluPM kXE5AbHJKkXq08tq3WighH5If2t2G4w0mJ9PrR0dSBqyqFjg3gFLvWJFNWolVchhdeQf fJmg== MIME-Version: 1.0 X-Received: by 10.49.17.98 with SMTP id n2mr5815536qed.61.1382120111975; Fri, 18 Oct 2013 11:15:11 -0700 (PDT) Received: by 10.96.41.99 with HTTP; Fri, 18 Oct 2013 11:15:11 -0700 (PDT) Received: by 10.96.41.99 with HTTP; Fri, 18 Oct 2013 11:15:11 -0700 (PDT) In-Reply-To: References: Date: Fri, 18 Oct 2013 20:15:11 +0200 Message-ID: Subject: Re: Entity cant be refreshed with new list values From: Romain Manni-Bucau To: users@tomee.apache.org Content-Type: multipart/alternative; boundary=047d7bea3f304fb48604e907ea3d X-Virus-Checked: Checked by ClamAV on apache.org --047d7bea3f304fb48604e907ea3d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Create a copy or use a dto Le 18 oct. 2013 20:06, "Jos=E9 Luis Cetina" a =E9cri= t : > I only want to add some info to a retrieved object but i dont want to > persist that new info > > > 2013/10/18 Jos=E9 Luis Cetina > > > But if use a transaction the new values will be committed and i want th= at > > values as a read only > > > > > > 2013/10/18 Romain Manni-Bucau > > > >> Cause you arent managed...do the whole method in an ejb (with a > >> transaction) > >> Le 18 oct. 2013 19:30, "Jos=E9 Luis Cetina" a > =E9crit > >> : > >> > >> > Ok, in the example i tried to set the list in the ejb, but if remove= d > >> 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 > >> > 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 rol= e > 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 objec= t, > >> is > >> > still the same even setting the value in the maged bean? > >> > > >> > > >> > > >> > 2013/10/18 Romain Manni-Bucau > >> > > >> > > Hi > >> > > > >> > > Your entity is not managed so you cant do it. It is normal. > >> > > Le 18 oct. 2013 19:09, "Jos=E9 Luis Cetina" = a > >> > =E9crit : > >> > > > >> > > > 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 ha= s > 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 = =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 > >> > > want > >> > > > 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 ro= le > >> 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 > >> > > > > >> > > > >> > > >> > > >> > > >> > -- > >> > ------------------------------------------------------------------- > >> > *SCJA. Jos=E9 Luis Cetina* > >> > ------------------------------------------------------------------- > >> > > >> > > > > > > > > -- > > ------------------------------------------------------------------- > > *SCJA. Jos=E9 Luis Cetina* > > ------------------------------------------------------------------- > > > > > > -- > ------------------------------------------------------------------- > *SCJA. Jos=E9 Luis Cetina* > ------------------------------------------------------------------- > --047d7bea3f304fb48604e907ea3d--