2

when doing userFacade.create(user) , i can see the user is created in the database , but i can't login with that user until i restart the application or if i unvalidate that session (it is in sessionScoped)

other exemple :if i create a Cv and add it to user CVs , i can't get the last cv created with user.getListCvs() even if it is in the database ,but if i restart the server i can see it

this is the bean of creating a CV of the current user

public class CreerCV implements Serializable{
  @EJB 
  private CvFacade cvFacade;
  @EJB 
  CandidatFacade candidatFacade;
  private Cv cv;
  private Candidat candidat;

  @PostConstruct
  public void init(){
    
    cv=new Cv();
 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    candidat=(Candidat)session.getAttribute("membre");
    candidat=candidatFacade.find(candidat.getId()); //so i can not get the listCVs of the my saved attribute in my session , but the listCVs of a user in the database ! This is the problem
}
//+getters and setters

public String creerCV(){
    if(candidat!=null){
        cv.setCandidat(candidat);
        candidat.getListeCv().add(cv);
        try{
            cvFacade.create(cv);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Création du cv avec succés"));
        }catch(Exception e) {
            System.out.println(e);
        }
    }
    else
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Une erreur s'est produite",""));
return "/index.xhtml";
}

and this is the bean of displaying the CVs of the current user

@Named
@RequestScoped
public class ListeCvs implements Serializable{
@EJB
private CvFacade cvFacade;

private List<Cv> listeCvs=new ArrayList<>();
private Cv selectedCv;
private Candidat candidat;


@PostConstruct
public void init(){
    
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    candidat=(Candidat)session.getAttribute("membre"); 
    System.out.println(candidat.getListeCv());
    if(candidat!=null)
    listeCvs=candidat.getListeCv();
    candidat=candidatFacade.find(candidat.getId());
}
//getters+setters ect..

Exemple :

i create a cv , i refresh i can not see the cv created in the table! i go to the database , i can see it is created i restart my application , i go to my table i can see my cv in the table

i create a second cv , i can not see it in the table , but i can see the previous one that i created,i see that it is created on the database,i restart my application go to my table i can see my 2 CVs there !

PS:for tests, i use System.out.print and some dataTables to see the list

Community
  • 1
  • 1
Sarah
  • 57
  • 1
  • 8

1 Answers1

1

How do you save CV? Are they saved and retrived by two different Entity Managers?

If so, check this thread about first level cache and refreshing.


How you do take care of transaction? If you create one, do you remember to commit and flush?

  1. Remember that if you use different EntityManagers for saving and retriving, they won't see the changes.
  2. Take care of your transaction and make sure that CV and USER are saved to database before you try to find them.

EDIT:

So, when you use find function, you receive totally different object of type Candidat, and when you run this part :

cv.setCandidat(candidat);
candidat.getListeCv().add(cv);

it works on different 'candidat' object, because of this line :

candidat=candidatFacade.find(candidat.getId());

If you want to keep every component with newest data in Candidat object, just edit them on THIS exact object that you passed from session. When you use find function, you receive new object, and every component has stale data. Try removing this finds from components.

EDIT2:

It goes like that. You retrive Candidate from session lets call it A. You retrieve it in CreerCV and ListeCV. Yeah it is the same object.

THEN you use find in CreerCV and receive different instance of class Candidate. Lets call it B.

THEN you use find in ListeCV and receive different instance of class Candidate. Lets call it C.

Ok now object B and C have same DATA but they are different objects for java.

You add something to list in object B, and expect to see this change in list from object C.

Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
  • i added the controlers – Sarah Apr 17 '18 at 15:10
  • the CV and user as i said in the question, are saved in the Database ! but when i do getListCv it does not work – Sarah Apr 17 '18 at 15:11
  • when i did candidat = session .getAttrib... , it did not receive the same object? so after this if i do find(candidat.id) it will retreive the same object in dataBase , and i can see the previous result but not the one i added check my exemple , i edited again my post – DetchKing Apr 17 '18 at 15:29
  • What I mean is. You get object A with same exact data as object B (that what happens when you run your two *finds*) then you add something to list in object A, of course you won't see this change in object B. They are two different objects – Emil Hotkowski Apr 17 '18 at 15:32
  • Alter account? I've been helping @Sarah – Emil Hotkowski Apr 17 '18 at 15:37
  • we're working toguether on the same project she's not here, so i'm answering in her place , it is legal right? and if u see , in the code there's A=B so at the end they are not different object right? – DetchKing Apr 17 '18 at 15:40
  • oh ! i do understand the problem , i think i need to reset the attribute of the Session right??? i need to do session.setAttribute(Candidat) so it will update when in i use it in the other beans ! – DetchKing Apr 17 '18 at 15:42
  • Try to remove finds, and see if it works, please : ) – Emil Hotkowski Apr 17 '18 at 15:43
  • it works without find of course, i know that ,i just wanted an explication to the problem , and now i understand it, thanks for your help , at the beggining i didn't understand you , that's why. – DetchKing Apr 17 '18 at 15:44
  • your EDIT2 is perfect ! – DetchKing Apr 17 '18 at 15:45
  • I am glad that we came to consensus. Happy coding! – Emil Hotkowski Apr 17 '18 at 15:45
  • Rijuk , you're a master ! All the team salutes you by the way :D – Sarah Apr 17 '18 at 15:51
  • what about adding candidatFacade.edit(candidat) to both classes after that? – DetchKing Apr 17 '18 at 16:11
  • We can continue conversation in here https://chat.stackoverflow.com/rooms/169179/candidate-for-sarah – Emil Hotkowski Apr 17 '18 at 16:26