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

Spring Java Discussion :

Cannot persist detached object [Data]


Sujet :

Spring Java

  1. #1
    Membre régulier
    Inscrit en
    Mai 2003
    Messages
    350
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 350
    Points : 84
    Points
    84
    Par défaut Cannot persist detached object
    J'ai deux classes
    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
     
    @SuppressWarnings( { "unused", "serial" })
    @Entity
    @Table(name = "Occupation")
     
    public class Occupation implements Serializable{
       @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
        @JoinColumn(name="LOGIN", unique = true,nullable = true)
        private Login login;
     
       --- 
     
      public Login getLogin() {
    		return login;
    	}
     
    	public void setLogin(Login login) {
    		this.login = login;
    	}
     
    }
     
    @SuppressWarnings( { "unused", "serial" })
    @Entity
    @Table(name = "Login")
     
    public class Login implements Serializable{
       @OneToOne(mappedBy = "login", fetch=FetchType.LAZY)
        private Occupation occupation;
     
       ---
     
       public Occupation getOccupation() {
    		return occupation;
    	}
     
    	public void setOccupation(Occupation occupation) {
    		this.occupation = occupation;
    	}
     
    }
    L'exécution
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Occupation o1 = new Occupation(new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2009"),new SimpleDateFormat("dd/MM/yyyy").parse("30/06/2009"));
            //Occupation o1 = new Occupation();
            o1.setLogin(l1);
            Occupation o2 = new Occupation(new SimpleDateFormat("dd/MM/yyyy").parse("01/07/2009"),new SimpleDateFormat("dd/MM/yyyy").parse("31/12/2009"));
            //Occupation o2 = new Occupation();
            o2.setLogin(l2);
            service.saveOccupations(new Occupation[] { o1, o2});
    et j'ai l'erreur suivante:
    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    [TopLink Info]: 2009.02.05 08:59:13.046--ServerSession(29751107)--TopLink, version: Oracle TopLink Essentials - 2.0 (Build b41-beta2 (03/30/2007))
    [TopLink Info]: 2009.02.05 08:59:13.421--ServerSession(29751107)--file:/C:/Netbeans/workspace/NetBeansProjects/HibernateTest/src/-jpa login successful
    Exception in thread "main" javax.persistence.EntityExistsException:
    Exception Description: Cannot persist detached object [P[1,jpa,jpa]].
    Class> mis.cinq.entites.Login Primary Key> [jpa]
    at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:198)
    at $Proxy16.persist(Unknown Source)
    at mis.cinq.dao.Dao.saveOccupation(Dao.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy19.saveOccupation(Unknown Source)
    at mis.cinq.service.Service.saveOccupations(Service.java:257)
    at mis.cinq.view.InitDB.fill(InitDB.java:97)
    at mis.cinq.view.InitDB.main(InitDB.java:36)
    Caused by: Exception [TOPLINK-7231] (Oracle TopLink Essentials - 2.0 (Build b41-beta2 (03/30/2007))): oracle.toplink.essentials.exceptions.ValidationException
    Exception Description: Cannot persist detached object [P[1,jpa,jpa]].
    Class> mis.cinq.entites.Login Primary Key> [jpa]
    at oracle.toplink.essentials.exceptions.ValidationException.cannotPersistExistingObject(ValidationException.java:2156)
    at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:3237)
    at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:323)
    at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3206)
    at oracle.toplink.essentials.mappings.ObjectReferenceMapping.cascadeRegisterNewIfRequired(ObjectReferenceMapping.java:655)
    at oracle.toplink.essentials.internal.descriptors.ObjectBuilder.cascadeRegisterNewForCreate(ObjectBuilder.java:1256)
    at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3212)
    at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:190)
    ... 21 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

  2. #2
    Membre régulier
    Profil pro
    Développeur Java
    Inscrit en
    Novembre 2008
    Messages
    63
    Détails du profil
    Informations personnelles :
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Novembre 2008
    Messages : 63
    Points : 109
    Points
    109
    Par défaut
    Bonjour,

    Je connais pas très bien jpa, mais c'est normal que tu n'est pas de @Id ni Occupation, ni sur Login ?
    Je croyais que c'était obligatoire et que ça servait à identifier un objet (peut être que toplink gère ça sans raler). Je vois aussi un :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    at oracle.toplink.essentials.exceptions.ValidationException.cannotPersistExistingObject(ValidationException.java:2156)
    Donc soit ton objet existe ou alors toplink s'embrouille avec les identifiants.

  3. #3
    Membre régulier
    Inscrit en
    Mai 2003
    Messages
    350
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 350
    Points : 84
    Points
    84
    Par défaut
    si j'en ait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    public class Occupation implements Serializable{
        @Id
    	@Column(nullable = false)
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	private int id;
     
    public class Login implements Serializable{
        @Id
    	@Column(length = 20,unique = true,nullable = false)
    	private String login;

  4. #4
    Membre régulier
    Inscrit en
    Mai 2003
    Messages
    350
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 350
    Points : 84
    Points
    84
    Par défaut
    En optant pour la mise à jour ça marche.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
     
    Occupation o1 = new Occupation(new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2009"),new SimpleDateFormat("dd/MM/yyyy").parse("30/06/2009"));
            //Occupation o1 = new Occupation();
            //o1.setLogin(l1);
            //o1.setPersonne(p1);
            //o1.setFonction(fadm);
            Occupation o2 = new Occupation(new SimpleDateFormat("dd/MM/yyyy").parse("01/07/2009"),new SimpleDateFormat("dd/MM/yyyy").parse("31/12/2009"));
            //Occupation o2 = new Occupation();
            //o2.setLogin(l2);
            //o2.setPersonne(p5);
            //o2.setFonction(fchf);
            service.saveOccupations(new Occupation[] { o1, o2});
            o1.setLogin(l1);
            o1.setPersonne(p1);
            o1.setFonction(fadm);
            o2.setLogin(l2);
            o2.setPersonne(p5);
            o2.setFonction(fchf);
            service.updateOccupations(new Occupation[] { o1, o2});
    Je me demande pourquoi l'autre code ne marche pas

  5. #5
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    EntityManager.persist c'est pour un objet nouveau (sans id…)
    si l'objet a été lu du repository et ensuite modifié il faut utiliser EntityManager.merge…

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Erreur "Cannot cast from Object to int"
    Par FatmaFafa dans le forum Débuter avec Java
    Réponses: 1
    Dernier message: 11/08/2012, 18h33
  2. [Mapping] @IdClass force le persist des objects associés
    Par dush_ dans le forum Hibernate
    Réponses: 0
    Dernier message: 20/05/2012, 14h44
  3. Problème cannot open shared object file sous ubuntu
    Par michelali dans le forum Boost
    Réponses: 0
    Dernier message: 20/05/2011, 12h08
  4. Cannot format given Object as a Date
    Par SAKDOSS dans le forum Collection et Stream
    Réponses: 2
    Dernier message: 20/02/2011, 16h38
  5. Cannot persist detached object
    Par coold dans le forum JPA
    Réponses: 2
    Dernier message: 04/02/2010, 14h33

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