Bonjour,
je débute en développement J2EE.
Je fais juste un formulaire avec un champs, que je souhaite stocker en base.
Et c'est la misère .
J'utilise JSP - EJB - JPA et serveur Glassfish et eclipse
J'ai installé Mysql, j'ai crée un pool de connexion dans glassfish, ça ping correctement.
Ensuite dans le projet JPA, dans mon fichier persistence
Il faut savoir j'ai testé avec un client standalone, je récupère bien un champs après une requête JPQL. Donc la connexion à la base s'effectue bien
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <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="SimpleJPA" transaction-type="JTA"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <jta-data-source>jdbc/adee</jta-data-source> <class>table.Groupe</class> </persistence-unit> </persistence>
dans le projet EJB :
et dans la page jsp :
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 package sample; import javax.annotation.Resource; import javax.ejb.*; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.PersistenceContext; import javax.persistence.EntityManager; import javax.transaction.UserTransaction; import table.Groupe; @Stateless(name="Example", mappedName="ejb/SimpleBeanJNDI") public class SimpleBeanImpl implements SimpleBean { public String sayHello(String name) { return "Hello " + name + "!"; } @PersistenceContext(unitName = "SimpleJPA") private EntityManager em; public Groupe createGroupe(Groupe groupe) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("SimpleJPA"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); em.persist(groupe); tx.commit(); em.close(); emf.close(); return groupe; } }
il faut savoir que si j'enleve bean.createGroupe(g), le résultat affiche bien dans le navigateur la valeur du champs saisi, j'aimerai juste l'ajouter dans ma base.
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 <body> <% //InitialContext context = new InitialContext(); //DataSource dataSource = (DataSource) context.lookup("jdbc/adee"); InitialContext ctx = new InitialContext(); SimpleBean bean = (SimpleBean) ctx.lookup("ejb/SimpleBeanJNDI"); String s1 = request.getParameter("num1"); String result = bean.sayHello(s1); Groupe g = new Groupe(); g.setNom(s1); bean.createGroupe(g); out.println(result); out.println("Database updated successfully"); %> </body>
et voici lors de l'envoi du formulaire, l'erreur affichée :
Donc si vous avez une idée, je suis preneur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 exception org.apache.jasper.JasperException: javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.RemoteException: null; nested exception is: java.lang.IllegalStateException: Exception Description: Cannot use an EntityTransaction while using JTA. root cause javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.RemoteException: null; nested exception is: java.lang.IllegalStateException: Exception Description: Cannot use an EntityTransaction while using JTA
Partager