IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

NetBeans Java Discussion :

EJB Session Bean MySQL


Sujet :

NetBeans Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2011
    Messages : 48
    Par défaut EJB Session Bean MySQL
    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

    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 + " ]";
        }
     
    }
    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
     
    /*
     * 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();
        }
     
    }
    persistabce.xml
    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

  2. #2
    Membre Expert
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2004
    Messages
    1 184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 184
    Par défaut
    Tu ne peux pas gérer à la main les transactions si tu déclare le mode JTA dans ton persistence.xml.

    C'est le container qui les gère en se basant sur les annotations de ton EJB TransactionAttribute.

  3. #3
    Membre averti
    Homme Profil pro
    Inscrit en
    Décembre 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2011
    Messages : 48
    Par défaut aide
    d’après ce que j'ai compris je dois modifier le fichier persistance.xml
    que dois-je faire cela exactement je suis débutant en java !

Discussions similaires

  1. [EJB] Session bean non instancié
    Par Erouan dans le forum Java EE
    Réponses: 13
    Dernier message: 24/08/2010, 18h08
  2. ejb session bean classCastException Deserialization
    Par bilou69 dans le forum Java EE
    Réponses: 2
    Dernier message: 09/06/2010, 17h29
  3. Réponses: 4
    Dernier message: 18/05/2010, 16h56
  4. [EJB Stateful] Fonctionnement des EJB Stateful Session Bean
    Par T`lash dans le forum Java EE
    Réponses: 3
    Dernier message: 15/04/2008, 02h10
  5. [Débutant] [lomboz] EJB Session
    Par VinceFromBcn dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 19/01/2005, 19h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo