Bonjour,

Mon EntityManager avec mon annotation @PersistenceContext est toujours null, je ne comprends pas pourquoi. Cela doit etre une betise

Voici la traceback:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
SEVERE: Servlet.service() for servlet springapp threw exception
java.lang.NullPointerException at com.mashup.dao.UserDAOImpl.findById(UserDAOImpl.java:42)
Et voici les differents fichiers concernes:
com.mashup.dao.UserDAOImpl.java EntityManager NullPointerException
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
@Stateless
public class UserDAOImpl {
	private EntityManager entityManager;	
 
	@PersistenceContext(unitName = "example")
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
 
	public EntityManager getEntityManager() {
		return entityManager;
	}
	public User findById(String i) {
 // /!\ Ici se trouve le nullPointerException /!\
 // /!\ Ici se trouve le nullPointerException /!\
 // /!\ Ici se trouve le nullPointerException /!\
		System.out.println(entityManager.toString());
 // /!\ Ici se trouvait le nullPointerException /!\
 // /!\ Ici se trouvait le nullPointerException /!\
 // /!\ Ici se trouvait le nullPointerException /!\
 
		log.debug("getting Users instance with id: " + i);
		try {
			User instance = entityManager.find(User.class, i);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
com.mashup.models.User.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
@Entity
public class User {
	@Id
	@GeneratedValue
	private Long id;
	private String name;
	private String login;
	private String password;
 
	// Avec les getters et les setters en dessous
com.mashup.controllers.HelloController.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public class HelloController implements Controller {
	private UserDAOImpl userDAOImpl;
 
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
 
		// Ce bloc fonctionne bien, la connexion est etablie
		// ----
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");
		EntityManager em = emf.createEntityManager();
 
		em.close();
		emf.close();
		// ----
 
		this.userDAOImpl.findById("0");
 
		return new ModelAndView("hello", "now", now);
	}
 
	public UserDAOImpl getUserDAOImpl() {
		return userDAOImpl;
	}
 
	public void setUserDAOImpl(UserDAOImpl userDAOImpl) {
		this.userDAOImpl = userDAOImpl;
	}
}
META-INF/persistence.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
	version="1.0">
	<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
		<properties>
			<property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml" />
			<property name="hibernate.c3p0.min_size" value="5" />
			<property name="hibernate.c3p0.max_size" value="20" />
			<property name="hibernate.c3p0.timeout" value="300" />
			<property name="hibernate.c3p0.max_statements" value="50" />
			<property name="hibernate.c3p0.idle_test_period" value="3000" />
		</properties>
	</persistence-unit>
</persistence>
WEB-INF/spring-app-servlet.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="userDAOImpl" class="com.mashup.dao.UserDAOImpl">
	</bean>
 
	<bean name="/hello.htm" class="com.mashup.controllers.HelloController">
		<property name="userDAOImpl" ref="userDAOImpl"/>
	</bean>
 
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
WEB-INF/applicationContext.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" />
 
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
 
	<tx:annotation-driven />
 
	<bean
		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcesser" />
 
	<context:annotation-config />
	<!--	<context:spring-configured />-->
</beans>
Merci