Problème JPA avec le "getEntityManager()"
Bonjour,
Développeur Java, je découvre JPA dans un environnement Struts, JSP et sous RAD.
J'ai génèré par JPA tout mes Entités et Bean Manager....
Mon Action sous Struts est celle-ci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
package com.ibm.project_democenter.actions;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.ibm.db.Country;
import com.ibm.db.controller.CountryManager;
public class CreateUserAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionMessages errors = new ActionMessages();
ActionForward forward = new ActionForward(); // return value
CountryManager test = new CountryManager();
List<Country> toto = test.getCountry();
// Finish with
return ("success");
}
} |
Mon fichier persistance.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="Project_DemoCenter">
<jta-data-source>java:comp/env/Connection_DEMO_DB</jta-data-source>
<class>com.ibm.db.Brand</class>
<class>com.ibm.db.Category</class>
<class>com.ibm.db.Country</class>
<class>com.ibm.db.Demo</class>
<class>com.ibm.db.DocumentaryType</class>
<class>com.ibm.db.Hardware</class>
<class>com.ibm.db.Imt</class>
<class>com.ibm.db.Iot</class>
<class>com.ibm.db.MinimalTime</class>
<class>com.ibm.db.News</class>
<class>com.ibm.db.Request</class>
<class>com.ibm.db.RequestType</class>
<class>com.ibm.db.Scenario</class>
<class>com.ibm.db.Software</class>
<class>com.ibm.db.SubCategory</class>
<class>com.ibm.db.TimeZone</class>
<class>com.ibm.db.Unaccess</class>
<class>com.ibm.db.User</class>
<class>com.ibm.db.UserRole</class>
<properties>
<property name="openjpa.jdbc.Schema" value="DEMO_DB"/>
</properties>
</persistence-unit>
</persistence> |
Et ma classe CountryManager qui a été génèré automatiquement, la voici :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
/**
*
*/
package com.ibm.db.controller;
import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import javax.persistence.Persistence;
import com.ibm.db.Country;
import java.util.List;
import javax.persistence.Query;
import java.lang.String;
/**
* @author Administrator
*
*/
@JPAManager(targetEntity=com.ibm.db.Country.class)
@SuppressWarnings("unchecked")
public class CountryManager {
private EntityManagerFactory emf;
public CountryManager() {
}
public CountryManager(EntityManagerFactory emf) {
this.emf = emf;
}
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManager getEntityManager() {
if (emf == null) {
//You can create an EntityManagerFactory as shown in the code below.
//If you create an EntityManagerFactory you must call the
//dispose method when you are done using it.
emf = Persistence.createEntityManagerFactory("Project_DemoCenter");
throw new RuntimeException(
"The EntityManagerFactory is null. This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
}
return emf.createEntityManager();
}
@NamedQueryTarget("getCountry")
public List<Country> getCountry() {
EntityManager em = getEntityManager();
List<Country> results = null;
try {
Query query = em.createNamedQuery("getCountry");
results = (List<Country>) query.getResultList();
} finally {
em.close();
}
return results;
}
} |
Mon souci vient de ma ligne de code
Code:
EntityManager em = getEntityManager()
Qui fait appel a getEntityManager()....mais quand c'est executé, j'ai l'erreur suivante :
Error 500: javax.servlet.ServletException: java.lang.RuntimeException: The EntityManagerFactory is null. This must be passed in to the constructor or set using the setEntityManagerFactory() method.
Je ne comprends pas pourquoi !
Merci de votre aide !