M.SMH
M.SMH

Reputation: 31

Insert into mysql with jsf and many to one relashionship

I wan't to insert into both Table "Participant" and Table "Avis" (the relationship between these table is one to many). When i run the app each time it displays an error. The insert in the two tables was okay, only the problem in the foreign key in the Table "Avis", it takes a null value. Realy i need help and i'm begineer in Java Developpemnt so it's may be a stupid mistake. Here is my managedBean:

@ManagedBean(name = "participBean") 
@RequestScoped 
public class ParticipBean implements Serializable { 
private static final long serialVersionUID = 1L; 
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml"); 
ParticipantService ParticipantServ=(ParticipantService);
context.getBean("ParticipantService"); 
AvisService AvisServ=(AvisService) context.getBean("AvisService"); 
private int idParticipant; 
private Integer idAvis; 
private Participant particip; 
private Avis avis; 
public Avis getAvis() { 
return avis; 
} 
public void setAvis(Avis avis) { 
this.avis = avis; 
} 
public int getId_avis() { 
return idAvis; 
} 
public void setId_avis(Integer idAvis) { 
this.idAvis = idAvis; 
} 
public Participant getParticip() { 
return particip; 
} 
public void setParticip(Participant particip) { 
this.particip = particip; 
} 
public int getId_interet() { 
return id_interet; 
} 
public void setId_interet(int id_interet) { 
this.id_interet = id_interet; 
} 
public Integer getId_participant() { 
return idParticipant; 
} 
public void setId_participant(Integer idParticipant) { 
this.idParticipant = idParticipant; 
} 
public ParticipBean() { 
particip=new Participant(); 
avis=new Avis(); 
} 
public void save(ActionEvent actionEvent){ 
Participant ad=ParticipantServ.findById(idParticipant); 
avis.setParticipant(ad); 
AvisServ.save(avis); 
ParticipantServ.save(particip); 
addMessage("Profil ajouté avec succès !"); 
avis=new Avis(); 
} 
} 
}

Any help please ? And this is the mapping Participant.java:

private Set<Avis>listeAvis=new HashSet<Avis>();
@OneToMany(mappedBy="participant",cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE})
public Set<Avis> getListeAvis() {
    return listeAvis;
}

public void setListeAvis(Set<Avis> listeAvis) {
    this.listeAvis = listeAvis;
}

Avis.java:

private Participant participant;
@ManyToOne(targetEntity=com.pfeAndroid.Beans.Participant.class)
@JoinColumn(name="id_participant")
public Participant getParticipant() {
    return participant;
}

public void setParticipant(Participant participant) {
    this.participant = participant;
}

Upvotes: 1

Views: 705

Answers (2)

M.SMH
M.SMH

Reputation: 31

I solved the problem of the NPE, and this is the correct code for the function save:

public void save(ActionEvent actionEvent){

                particip.setIdParticipant(idParticipant);
                avis.setParticipant(particip);
                particip.getListeAvis().add(avis);
                ParticipantServ.save(particip);
                AvisServ.save(avis); 

                addMessage("Profil ajouté avec succès !"); 
                particip=new Participant();
                avis=new Avis(); 
            }

Upvotes: 1

Skizzo
Skizzo

Reputation: 2983

I think that your trouble is that you are trying to salve the avis object first to save the pertecipant object. Then depending on your configuration you have define a cascade polite that consent you to save only the partecipant object. Summarizing you can do:

    public void save(ActionEvent actionEvent){ 
    Participant ad=ParticipantServ.findById(idParticipant); 
    avis.setParticipant(ad); 
    ParticipantServ.save(particip); 
    AvisServ.save(avis); 
    addMessage("Profil ajouté avec succès !"); 
    avis=new Avis(); 
}

or

    public void save(ActionEvent actionEvent){ 
    Participant ad=ParticipantServ.findById(idParticipant); 
    ad.getListeAvis().add(avis)
    ParticipantServ.save(particip); 

    addMessage("Profil ajouté avec succès !"); 
    avis=new Avis(); 
}

Upvotes: 1

Related Questions