Bonjour,
je travaille sur un mini projet java et j'ai toujours l 'erreur suivant
java.lang.IllegalStateException:
Exception Description: Cannot use an EntityTransaction while using JTA.
j'ai fait un projet j2ee entreprise application
voila mon entity bean
voila mon session bean
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pack.test; import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; /** * * @author hamza */ @Entity @Table(name = "personne", catalog = "bourse", schema = "") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Personne.findAll", query = "SELECT p FROM Personne p"), @NamedQuery(name = "Personne.findByNum", query = "SELECT p FROM Personne p WHERE p.num = :num"), @NamedQuery(name = "Personne.findByNom", query = "SELECT p FROM Personne p WHERE p.nom = :nom")}) public class Personne implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Column(name = "num", nullable = false) private Integer num; @Basic(optional = false) @NotNull @Size(min = 1, max = 20) @Column(name = "nom", nullable = false, length = 20) private String nom; public Personne() { } public Personne(Integer num) { this.num = num; } public Personne(Integer num, String nom) { this.num = num; this.nom = nom; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } @Override public int hashCode() { int hash = 0; hash += (num != null ? num.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Personne)) { return false; } Personne other = (Personne) object; if ((this.num == null && other.num != null) || (this.num != null && !this.num.equals(other.num))) { return false; } return true; } @Override public String toString() { return "pack.test.Personne[ num=" + num + " ]"; } }
persistabce.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pack.test; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.PersistenceContext; /** * * @author hamza */ @Stateless public class sessiontest implements sessiontestLocal { @PersistenceContext(unitName = "TestEjb-ejbPU") private EntityManager em; @Override public void persist(Object object) { em.persist(object); } @Override public void creerpersonne(int num, String nom) { Personne p = new Personne(num,nom); EntityManagerFactory emf=Persistence.createEntityManagerFactory("TestEjb-ejbPU"); em=emf.createEntityManager(); em.getTransaction().begin(); em.persist(p); em.getTransaction().commit(); em.close(); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 <?xml version="1.0" encoding="UTF-8"?> <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="TestEjb-ejbPU" transaction-type="JTA"> <jta-data-source>boursecnx</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties/> </persistence-unit> </persistence>
index.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 <%-- Document : index Created on : 23 déc. 2011, 03:42:10 Author : hamza --%> <%@page import="pack.test.sessiontest"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <% sessiontest objs = new sessiontest(); objs.creerpersonne(5, "jj"); %> </body> </html>
aidez moi SVP a trouver d’où arrive l erreur sachant que je travail sur une base de donnée mysql
Partager