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

Java EE Discussion :

Illegal to call this method from injected, managed EntityManager


Sujet :

Java EE

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité(e)
    Invité(e)
    Par défaut Illegal to call this method from injected, managed EntityManager
    Bonjour,

    cette question est à cheval entre JPA et ejb3.

    j'ai mis en place une classe manager qui implémente un EntityManager.
    JE voudrais que mon manager mette à jour des données en base de données mais ça coince.

    j'ai déjà effectué l'ajout et la suppression mais pour l'update je ne comprend pas pourquoi rien ne fonctionne.

    dans mes lecture voila ce que j'ai trouvé pour effectuer un update:
    http://www.winstonprakash.com/articl...te_Delete.html

    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
     
    public boolean updateUser(User user) {
      EntityManager em = getEntityManager();
      try{
        em.getTransaction().begin();
        User userx = em.find(User.class, user.getUserId());
        userx.setUserName(user.getUserName()); 
        userx.setPasswd(user.getPasswd());
        userx.setEmailAddress(user.getEmailAddress());
        em.getTransaction().commit();
      } finally {
        em.close();
        return false;
      }
    }
    on retrouve le même type d'exemple dans le livre intitulé Pro EJB3 Java Persistence API

    j'ai appliqué le même principe mais j'ai une erreur de relevée

    voici mon code:

    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
     
    ....
     
    	@PersistenceContext(unitName="ToDoEJB3")
    	private EntityManager entityManager;
     
     
        //Méthode CRUD
     
        //REFRESH
     
        //UPDATE
        public void update(int id) {
     
        	entityManager.getTransaction().begin();
        	entityManager.find(Taches.class, id).setCommentaire("update effectue");
        	entityManager.getTransaction().commit();
        }
     
    ....
    voici l'erreur relevée:

    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
     
    Exception in thread "main" javax.ejb.EJBException: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
    	at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
    	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
    	at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
    	at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:304)
    	at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
    	at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
    	at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:769)
    	at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:573)
    	at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:373)
    	at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:166)
    Caused by: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
    	at org.jboss.ejb3.entity.TransactionScopedEntityManager.getTransaction(TransactionScopedEntityManager.java:226)
    	at bean.session.manager.TachesManager.update(TachesManager.java:41)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
    	at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
    	at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
    	at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
    	at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:304)
    	at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
    	at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
    	at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:769)
    	at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:573)
    	at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:373)
    	at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:166)
    	at org.jboss.remoting.MicroRemoteClientInvoker.invoke(MicroRemoteClientInvoker.java:163)
    	at org.jboss.remoting.Client.invoke(Client.java:1634)
    	at org.jboss.remoting.Client.invoke(Client.java:548)
    	at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:62)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:67)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:53)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:74)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:107)
    	at $Proxy0.update(Unknown Source)
    	at client.Client4.main(Client4.java:62)
    	at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:74)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:67)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:53)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:74)
    	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    	at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:107)
    	at $Proxy0.update(Unknown Source)
    	at client.Client4.main(Client4.java:62)
    quelqu'un aurait il une idée ?

    merci beaucoup

  2. #2
    Invité(e)
    Invité(e)
    Par défaut
    bon ba j'ai trouvé une solution en tatonant et je dois dire que je comprend pas vraiment alors même si maintenant cela fonctionne, quelqu'un pourrait il m'expliquer pourquoi cela fonctionne ainsi

    procédé:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
        //UPDATE
        public void update(int id) {
     
        	entityManager.find(Taches.class, id).setCommentaire("update effectue");
        }
    j'ai fais tout simplement un set sur mon find et la mise à jour est automatique !

    quelqu'un peut il m'expliquer le procédé ?
    pourquoi cela fonctionne -il ainsi ?
    jem'attendais à utiliser une méthode spécial comme persit ou remove à partir de l'entityManager.

    voila

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    75
    Détails du profil
    Informations personnelles :
    Âge : 36
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2008
    Messages : 75
    Par défaut
    bonjour,
    c'est un peu retard désolé.....juste pour les autres qui vont avoir la meme question ,
    je crois pour faire la mise à jour il faut faire entityManager.merge(yourObject) ;
    ++

  4. #4
    Membre Expert Avatar de nathieb
    Homme Profil pro
    DevOps
    Inscrit en
    Mai 2004
    Messages
    1 058
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : DevOps
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 058
    Par défaut Il faut raccrocher ton entity
    Bonjour,

    La solution est simple, quand tu récupéres une entité JPA. dans certains
    types d'architecture elle se détache de son contexte et oui. Donc pour effectuer un update, il faut se créer un nouveau contexte ou tu peux faire un mise à jour d'ou le besoin de find . Je trouve l'entité avec son contexte et je peux faire ainsi une mise à jour sur un objet existant.

    olivier

Discussions similaires

  1. Réponses: 4
    Dernier message: 25/02/2009, 12h10
  2. Call Remote EJB3 from Different Host
    Par ForHuman dans le forum NetBeans
    Réponses: 3
    Dernier message: 07/01/2009, 18h30
  3. ORA-19511 Error received from media manager layer
    Par pierho dans le forum Administration
    Réponses: 1
    Dernier message: 11/07/2008, 08h39
  4. Call stored procedure from trigger
    Par DAGADA dans le forum SQL
    Réponses: 3
    Dernier message: 24/05/2007, 13h33

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