I've been on this problem for more than a week...
I'm pretty new to the world of JaveEE, Hibernate, Glassfish, etc.
I'm using JavaEE/Glassfish and Hibernate to create a little project for school.
1) We're supposed to get a list of flights from a local database using Hibernate.
2) We're supposed to get the result through a GET request using a REST-api
My "routing" class :
package routes;
import DAO.DAOUtilisateurs;
import DAO.DaoVolsManager;
import controllers.VolsBD;
import models.Vol;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
@Path("/vols")
public class Vols {
DaoVolsManager manager = new DaoVolsManager();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Vol> getVol() {
return manager.getAllVols();
}
}
Which refers to my flights entity manager class DAOVolsManager
:
package DAO;
import com.sun.istack.NotNull;
import models.Vol;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class DaoVolsManager {
// Create an EntityManagerFactory when you start the application.
private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence
.createEntityManagerFactory("VolDB");
/**
* Renvoie tous les vols de la base
* @return Liste de vols
*/
public List<Vol> getAllVols() {
List<Vol> vols = new ArrayList<>();
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
try {
vols = manager.createQuery("FROM Vol", Vol.class).getResultList();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return vols;
}
}
Hibernate persistence.xml
file :
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="VolDB" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Entity classes -->
<class>models.Vol</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/vols" />
<property name="javax.persistence.jdbc.user" value="**myusername**" />
<property name="javax.persistence.jdbc.password" value="**mypassword**" />
</properties>
</persistence-unit>
</persistence>
Here come's the problem : when I'm getting the data in a Main
class ...
import models.Vol;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
public class Main {
// Create an EntityManagerFactory when you start the application.
private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence
.createEntityManagerFactory("VolDB");
public static void main(String[] args) {
// Print all the Students
List<Vol> vols = readAll();
if (vols != null) {
for (Vol v : vols) {
System.out.println(v.getNumVol());
}
}
// NEVER FORGET TO CLOSE THE ENTITY_MANAGER_FACTORY
ENTITY_MANAGER_FACTORY.close();
}
/**
* Read all the Students.
*
* @return a List of Vols
*/
public static List readAll() {
List vols = null;
// Create an EntityManager
EntityManager manager = ENTITY_MANAGER_FACTORY.createEntityManager();
EntityTransaction transaction = null;
try {
// Get a transaction
transaction = manager.getTransaction();
// Begin the transaction
transaction.begin();
// Get a List of Students
vols = manager.createQuery("SELECT v FROM Vol v",
Vol.class).getResultList();
// Commit the transaction
transaction.commit();
} catch (Exception ex) {
// If there are any exceptions, roll back the changes
if (transaction != null) {
transaction.rollback();
}
// Print the Exception
ex.printStackTrace();
} finally {
// Close the EntityManager
manager.close();
}
return vols;
}
}
... everything works fine and data is showing in local console but once I try to get the data by launching my Glassfish server and typing the good url in the browser I'm getting an Error 500: Internal Server Error
saying javax.persistence.PersistenceException: No Persistence provider for EntityManager named VolDB
I tried to move my persistence.xml
in different locations with no success.
What am I supposed to do to avoid that error and get Hibernate to work with Glassfish ? Finally, once I will solve that problem what should I be aware of to make that work on a production server ?
EDIT : Added the Hibernate libraries to Glassfish in Intellij Project Structure > Artifacts
and finally it works !