Reputation: 327
I am having the following project:
The file model.Clienti is a "JPA Entity", main.Main is a pojo with a main method and it's the client for the model.Clienti file. main.Main has the following code:
package main;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.Clienti;
public class Main {
public static void main(String[] args) {
EntityManager entityManager = Persistence.createEntityManagerFactory("JPATestEJB").createEntityManager();
List<Clienti> list = entityManager.createQuery("select c from Clienti c", Clienti.class).getResultList();
for (Clienti clienti : list) {
System.out.println(clienti.getNume());
}
}
}
And it works without problems, outputing exactly what I have in the database.
So 'locally' i can use the JPA file without problems, yet when I try to create an EJB and EJB client to do the same thing as main.Main file I get an error.
The EJB is located in the pachet.EmployeeSession (the interface)
package pachet;
import javax.ejb.Remote;
@Remote
public interface EmployeeSession {
public String getEmplastname(Integer empno);
public String test();
}
And the EmployeeSessionBean
package pachet;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.Clienti;
@Stateless
public class EmployeeSessionBean implements EmployeeSession {
public String getEmplastname(Integer empno) {
try {
EntityManager em = Persistence.createEntityManagerFactory("JPATestEJB").createEntityManager();
List<Clienti> list = em.createQuery("select c from Clienti c", Clienti.class).getResultList();
for (Clienti clienti : list) {
System.out.println(clienti.getNume());
}
} catch (EJBException e) {
e.printStackTrace();
}
return "a mers";
}
public String test() {
return "works";
}
}
For this EJB i've created a standalone client, in another project.
The client has only one class, the following:
import javax.naming.InitialContext;
import pachet.EmployeeSession;
public class Main {
public static void main(String[] args) {
try {
InitialContext ic = new InitialContext();
EmployeeSession thing = (EmployeeSession) ic.lookup("pachet.EmployeeSession");
System.out.println(thing.test());
System.out.println("It seems it runs: " + thing.getEmplastname(1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Yet when I try to run the Main (this one from the Default package) I get the following output
As it can be seen there is no problem connecting to the EJB since the thing.test(); which returns "works" definetly ran, there is no problem regarding the required libraries for the client i think, but after that an exception ocures.
The continuation as it can be seen EclipseLink doesn't find com.mysql.jdbc.Driver.
So the question is: how can it be possible that the main.Main in the EJB project run correctly and display the info from the db, yet Main from the ejb client doesn't.
More precisely the problem is in the EJB EmployeeSessionBean, if it had the following structure:
package pachet;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.Clienti;
@Stateless
public class EmployeeSessionBean implements EmployeeSession {
public String getEmplastname(Integer empno) {
try {
String b = "nothing";
} catch (EJBException e) {
e.printStackTrace();
}
return "a mers";
}
public String test() {
return "works";
}
has the following output: (note that I removed all the JPA 'business' code)
}
and it can be seen that both methods invocated from the client worked. This means the problem is because the ejb EmployeeSessionBean can't make use of the JPA the same as main.Main file but the question is why? They're definetly in the same project and it can be seen i have the required libraries EclipseLink and mysql-jdbc.
If one of them would've been wrong then main.Main wouldn't be able to connect as well, right? ' Thanks for the time.
Here's a look at my persistence.xml file though it seems to work ok. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<class>model.Clienti</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/comenzi"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
Upvotes: 0
Views: 1398
Reputation: 18389
You need to have the MySQL jar deployed to your Glassfish server. Either deploy it with your app, or put it in the applib directory on the server.
Normally in Glassfish a server DataSource would be used instead of JDBC directly, so you may want to try that as well.
Upvotes: 1