Bonjour
Je développe une simple application avec les EJB3
Pour mon application j’ai définis un EJB Entity Employe
puis j'ai définis un bean de type session stateless
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
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
65 package ejb.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; @Entity @Table (name="employe") public class Employe implements Serializable{ private int matricule; private String nom; private String prenom; private String adresse; private float salaire; private int age; public Employe(){ } @Id public int getMatricule() { return matricule; } public void setMatricule(int matricule) { this.matricule = matricule; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getAdresse() { return adresse; } public void setAdresse(String adresse) { this.adresse = adresse; } public float getSalaire() { return salaire; } public void setSalaire(float salaire) { this.salaire = salaire; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
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
32
33
34
35
36
37
38
39
40
41
42
43 package ejb.session; import java.util.ArrayList; import ejb.entity.Employe; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.jboss.annotation.ejb.LocalBinding; import org.jboss.annotation.ejb.RemoteBinding; import ejb.session.EmployeBeanDAOLocal; @Stateless @Local({EmployeBeanDAOLocal.class}) @LocalBinding(jndiBinding="EmployeBeanDAO/Local") @Remote({EmployeBeanDAOLocal.class}) @RemoteBinding(jndiBinding="EmploeBeanDAO/Remote") public class EmployeBeanDAO implements EmployeBeanDAORemote, EmployeBeanDAOLocal { @PersistenceContext (unitName="project") private EntityManager em; public EmployeBeanDAO() { } public void add(Employe emp){ em.persist(emp); } public Employe find(int matricule){ return em.find(Employe.class,matricule); } }
j'ai crée la table "employe" dans base de données PostgresSQL, et j'ai crée un profil de connexion "profile", et au moment de création du projet j'ai bien associé le "profil" au projet EJB
en fin j'ai utilisé une servlet pour tester les EJB3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 package webControler; import java.io.IOException; import java.io.PrintWriter; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ejb.entity.Employe; import ejb.session.*; public class AddEmploye extends HttpServlet { public AddEmploye() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("<html><head><title>Gestion d'employes</title></head><body>"); try { Context contex=new InitialContext(); EmployeBeanDAOLocal emp=(EmployeBeanDAOLocal)contex.lookup("EmployeBeanDAO/Local"); Employe e=new Employe(); e.setAdresse("xxx"); e.setAge(23); e.setNom("yyyyy"); emp.add(e); } catch(Exception e){ e.printStackTrace(out); out.println("<h2>ERREUR="+e.getMessage()+"</h2></body></html>"); } } }
l'erreur que c'est à l'exécution il m'affiche
EmployeBeanDAO not bound
je ne sais pas ou es l'erreur
voila aussi le fichier de configuration persistance.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 <?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" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <non-jta-data-source>java:profile</non-jta-data-source> <class> ejb.entity.Employe</class> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> </properties> </persistence-unit> </persistence>
Partager