Hot questions for Using Enterprise JavaBeans in intellij idea

Question:

I have a simple EJB:

@Stateless
public class EJB {

    public void aMethod() {
        // ...
        Event event = createEvent();
        // ...
    }

    private static Event createEvent() {
        Event event = new Event();
        // ...
        return event;
    }
}

IntelliJ Ultimate shows an error because there is a static method inside an EJB.

Using JBoss EAP the EJB works fine. Does the EJB spec forbids using (private) static "helper" methods? Why? And why does it work with JBoss EAP (and most likely other application servers)?


Answer:

Does the EJB spec forbids using (private) static "helper" methods?

No, it does not. It does forbid the methods that make up EJBs' interface with the container to be static, whether those methods are identified by method naming patterns or by annotations. It also forbids EJBs to read or write fields that are static but not final. But it does not place a blanket prohibition on EJBs having or calling static methods.

And why does it work with JBoss EAP (and most likely othe application servers)?

Because it should. As far as I can tell, it is IntelliJ that is wrong here. I speculate that IntelliJ may be confused by the name of the particular method it is flagging, because of the appearance of "create" in it. This comes a bit close to, but does not actually match, some of the old-school method name patterns that are significant to EJB.

Question:

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;

Question:

How to setup in IntelliJ IDEA VM options.

I need to setup

set JAVA_OPTS="-Dtomee.serialization.class.whitelist=" 
set CATALINA_OPTS="-Dtomee.serialization.class.blacklist=-"

In run/debug configuration for my local tomee server I pass into VM options

-tomee.serialization.class.whitelist=*
-tomee.serialization.class.blacklist=-

also tried without *. It still giving me "invalid arguments" error after run/debug the server. How to set it properly?

ps.If I run the server manually everything works correct.


Answer:

You need to specify the two properties as Java system properties (-D) in the VM options of your run configuration:

-Dtomee.serialization.class.whitelist=*
-Dtomee.serialization.class.blacklist=-