Domanda

i'm working on an ejb 3.0 tutorial and i want to add data to the database when i set run it doesn't indicate any error and it doesn't add the following data in the db either : this is the session bean code :

public String creerCompte(CompteBancaire c) {

 try{
  em.persist(c);

  return null;

  }catch (Exception ex) {
        return ex.getLocalizedMessage();
    }
}
public static void main(String[] args) throws Exception{
GestionnaireDeCompteBancaire g=new GestionnaireDeCompteBancaire();

CompteBancaire c=new CompteBancaire("John Lennon", 150000);
g.creerCompte(c);}

the method main just to try the method creercompte out

È stato utile?

Soluzione

It doesn't indicate any error because you catch the error it throws, return its localized message to the caller, and the caller completely ignores the returned message. Remove the catch (Exception) block completely, re-run your code, and you'll probably get a NullPointerException.

A session bean is not supposed to be instantiated using new GestionnaireDeCompteBancaire(). It's supposed to be packaged inside a Java EE application, which is deployed inside a Java EE container like JBoss or Glassfish, and then invoked using a web application deployed in this same container or using a Java EE client application. I think you really need to read a book or a serious tutorial about EJBs (and exception handling).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top