openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From José Luis Cetina <maxtorz...@gmail.com>
Subject Entity cant be refreshed with new list values
Date Fri, 18 Oct 2013 17:03:04 GMT
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message