Darknesshadow Darknesshadow - 1 month ago 7
Java Question

Cannot resolve reference Local ejb-ref

I have an issue in programming an EJB application. I search a solution but I still have the same problem in intelliJ with Glassfish4 :
" Cannot resolve reference [Local ejb-ref name=EJB.AdminEJB,Local 3.x interface =Interface.AdminInterface,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] because there are [2] ejbs in the application with interface Interface.AdminInterface."
And excuse-me for my english, I'm french.

AdminInterface in a package Interface

@Local
public interface AdminInterface {
public void creerParieur(Parieur parieur);

public void supprimerParieur (String login);

public void creerBookmaker(Bookmaker bookmaker);

public void supprimerBookmaker (String login);

public void modifParieur (Parieur parieur);

public void modifBookmaker (Bookmaker bookmaker);

public void ajouterCote(Cote cote);

public void ajouterMatch (Match match);

public List<Cote> listeCote(String log);

public List<Match> listeMatch();

public List<Parieur> listeParieur();

public List<Bookmaker> listeBookmaker();

public Parieur rechercheParieur(String id);

public Bookmaker rechercheBookmaker (String id);

public void setLogin(String login);

public String getLogin();


}

AdminEJB in a package EJB

@Stateless
public class AdminEJB implements AdminInterface{
String login;
String mdp;
@PersistenceContext(unitName = "NewPersistenceUnit")
EntityManager em;

public AdminEJB(){}

public String getLogin(){
return login;
}

public void setLogin(String login){
this.login=login;
}

public String getMdp(){
return mdp;
}

public void setMdp(String mdp){
this.mdp=mdp;
}

public void creerParieur(Parieur parieur){
em.persist(parieur);
}

public void supprimerParieur(String login){
Parieur parieur=new Parieur ();
Query req=em.createQuery("select OBJECT(P) from Parieur P where P.login=:login");
req.setParameter("login", login);
parieur=(Parieur)req.getSingleResult();
em.remove(parieur);
}

public void modifParieur(Parieur parieur){
em.merge(parieur);
}

public List<Parieur> listeParieur(){
Query req=em.createQuery("select OBJECT(P) from Parieur P");
return req.getResultList();
}

public void creerBookmaker(Bookmaker bookmaker){
em.persist(bookmaker);
}

public void supprimerBookmaker(String login){
Bookmaker bookmaker;
Query req=em.createQuery("select OBJECT(B) from Bookmaker B where B.pseudo=:login");
req.setParameter("login", login);
bookmaker=(Bookmaker)req.getSingleResult();
em.remove(bookmaker);
}

public void modifBookmaker(Bookmaker bookmaker){
em.merge(bookmaker);
}

public List<Bookmaker> listeBookmaker(){
Query req=em.createQuery("select OBJECT(B) from Bookmaker B");
return req.getResultList();
}

public List<Match> listeMatch(){
Query req=em.createQuery("select OBJECT(M) from Match M");
return req.getResultList();
}

public Bookmaker rechercheBookmaker(String id){
return em.find(Bookmaker.class,id);
}

public Parieur rechercheParieur(String id){
return em.find(Parieur.class,id);
}

public void ajouterCote (Cote cote){
em.persist(cote);
}

public void ajouterMatch (Match match){
em.persist(match);
}

public List<Cote> listeCote(String log){
Bookmaker bookmaker = new Bookmaker();
bookmaker = this.rechercheBookmaker(log);
Query req = em.createQuery("select OBJECT(C) from Cote C where C.bookmaker=:bookmaker");
req.setParameter("bookmaker", bookmaker);
return req.getResultList();
}


}

ControlerBean in a package ManagedBean

@ManagedBean
@RequestScoped
public class ControlerBean implements Serializable{
Bookmaker bookmaker;
Pari pari;
Parieur parieur;
Match match;
Cote cote;
String nomObjetP;
String nomEnP;
String pseudoUser;
String pwdUser;

@EJB
private AdminInterface admin;

public ControlerBean(){
bookmaker = new Bookmaker();
parieur = new Parieur();
cote = new Cote();
match= new Match();
pari= new Pari();

}

public String getNomObjetP() {
return nomObjetP;
}

public void setNomObjetP(String nomObjetP) {
this.nomObjetP = nomObjetP;
}

public String getNomEnP() {
return nomEnP;
}

public void setNomEnP(String nomEnP) {
this.nomEnP = nomEnP;
}

public Pari getPari() {
return pari;
}

public void setPari(Pari pari){
this.pari=pari;
}

public Bookmaker getBookmaker() {
return bookmaker;
}

public void setBookmaker(Bookmaker bookmaker) {
this.bookmaker = bookmaker;
}

public Parieur getParieur() {
return parieur;
}

public void setParieur(Parieur parieur) {
this.parieur = parieur;
}

public Cote getCote() {
return cote;
}

public void setCote(Cote cote) {
this.cote = cote;
}

public Match getMatch(){
return match;
}

public void setMatch(Match match){
this.match=match;
}

public AdminInterface getAdmin() {
return admin;
}

public void setAdmin(AdminInterface admin) {
this.admin = admin;
}

public String getPseudoUser() { return pseudoUser; }

public void setPseudoUser(String pseudoUser) {
this.pseudoUser = pseudoUser;
}

public String getPwdUser() {
return pwdUser;
}

public void setPwdUser(String pwdUser) {
this.pwdUser = pwdUser;
}

public String addParieur(){
parieur.setArgent(1000);
admin.creerParieur(parieur);
return "OK";
}

public String modifParieur(){
admin.modifParieur(parieur);
return "OK";
}

public String supprParieur(){
admin.supprimerParieur(parieur.getLogin());
return "OK";
}

public String addBookmaker(){
admin.creerBookmaker(bookmaker);
return "OK";
}

public String modifBookmaker(){
admin.modifBookmaker(bookmaker);
return "OK";
}

public String supprBookmaker(){
admin.supprimerBookmaker(bookmaker.getPseudo());
return "OK";
}

public List<Bookmaker> listeBookmaker(){
return admin.listeBookmaker();
}

public List<Parieur> listeParieur(){
return admin.listeParieur();
}

public List<Match> listeMatch(){ return admin.listeMatch(); }

public String addCote(){
pseudoUser = admin.getLogin();
cote.setBookmaker(admin.rechercheBookmaker(pseudoUser));
admin.ajouterCote(cote);
return "OK";
}

public String addMatch(){
admin.ajouterMatch(getMatch());
return "OK";
}


}

Thank's very much for any help

Answer

When you have two EJBs implementing the same interface they need to be differentiated so that the container knows which one to inject.

Add the name parameter in the @Stateless annotation to all beans implementing the same interface. In the @EJB annotation, use the beanName parameter to inject the appropriate session bean implementation.

@Stateless(name="AdminEJB1") 
public class AdminEJB implements AdminInterface { .... }

@EJB(beanName = "AdminEJB1") 
private AdminInterface myAdminEjb;

You can also skip the name parameter in the @Stateless annotation and then use the name of the implementing class as the beanName parameter in the @EJB annotation.

@Stateless
public class AdminEJB implements AdminInterface { .... }

@EJB(beanName = "AdminEJB") 
private AdminInterface myAdminEjb;
Comments