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

JPA Java Discussion :

impossible a persister


Sujet :

JPA Java

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    506
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 506
    Par défaut impossible a persister
    salut,

    je developpe sous netbean une petite application test qui consiste a persister 2 tables en relation one to many unidirectionnel dans une base de données derby embarquée.

    voici le code source de ma table eleve :

    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
    98
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package testjpa;
     
    import java.io.Serializable;
    import java.util.List;
    import javax.persistence.*;
     
     
    /**
     *
     * @author modde
     */
    @Entity
    @Table(name="eleve")
    public class Eleve implements Serializable {
     
     
        private static final long serialVersionUID = 1L;
        @Id
        @Column(name="ideleve")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
     
        @Column(name="nom",nullable=false)
        private String nom="";
     
        @Column(name="prenom",nullable=false)
        private String prenom="";
     
        @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="eleve")
        @JoinTable(name="note")
     
        private List<Note> note;
     
        public String getNom() {
            return nom;
        }
     
        public List<Note> getNote() {
            return note;
        }
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public void setNote(List<Note> note) {
            this.note = note;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
     
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.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 Eleve)) {
                return false;
            }
            Eleve other = (Eleve) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "testjpa.Eleve[id=" + id + "]";
        }
     
    }
    le code de ma table note :
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package testjpa;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
     
    /**
     *
     * @author modde
     */
    @Entity
    @Table(name="note")
    public class Note implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @Column(name="idnote")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
     
        @Column(name="note")
        private String note="";
     
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinTable(name="eleve")
     
        private Eleve eleve;
     
        public Eleve getEleve() {
            return eleve;
        }
     
        public String getNote() {
            return note;
        }
     
        public void setEleve(Eleve eleve) {
            this.eleve = eleve;
        }
     
        public void setNote(String note) {
            this.note = note;
        }
     
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.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 Note)) {
                return false;
            }
            Note other = (Note) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "testjpa.Note[id=" + id + "]";
        }
     
    }
    voici le code pour la persistance :
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package testjpa;
     
    import java.util.ArrayList;
    import java.util.List;
    import javax.persistence.*;
    /**
     *
     * @author modde
     */
    public class Test {
     
     
        private EntityManager manager;
        private EntityManagerFactory factory;
     
     
        public Test(){
     
        }
     
        public void Start(){
     
            factory =Persistence.createEntityManagerFactory("Test");
            manager =factory.createEntityManager();
        }
     
        public void Stop(){
            manager.close();
            factory.close();
        }
     
        public void testPersistance() {
     
            manager.getTransaction().begin();
     
     
            List list =new ArrayList<Note>();
     
            Eleve eleve =new Eleve();
     
            eleve.setId(1L);
            eleve.setNom("Ozzy");
            eleve.setPrenom("Dente");
     
            Note note =new Note();
     
            note.setNote("15");
            note.setEleve(eleve);
     
            list.add(note);
     
            eleve.setNote(list);
     
            manager.persist(eleve);
     
           manager.flush();
            manager.getTransaction().commit();
            manager.getTransaction().rollback();
     
     
     
        }
     
    }
    l'erreur :
    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
    98
    99
    100
    101
    102
    103
    104
    105
    106
    run:
    [EL Info]: 2010-03-22 22:48:41.258--ServerSession(15020576)--EclipseLink, version: Eclipse Persistence Services - 2.0.0.v20091127-r5931
    [EL Info]: 2010-03-22 22:48:42.858--ServerSession(15020576)--file:/home/modde/NetbeanProjets/TestJpa/src/_Test login successful
    [EL Warning]: 2010-03-22 22:48:43.417--ServerSession(15020576)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: Table/View 'ELEVE' existe déjà dans Schema 'TEST'.
    Error Code: 30000
    Call: CREATE TABLE eleve (ideleve BIGINT NOT NULL, prenom VARCHAR(255) NOT NULL, nom VARCHAR(255) NOT NULL, eleve_ideleve BIGINT NOT NULL, Note_idnote BIGINT NOT NULL, PRIMARY KEY (ideleve, eleve_ideleve, Note_idnote))
    Query: DataModifyQuery(sql="CREATE TABLE eleve (ideleve BIGINT NOT NULL, prenom VARCHAR(255) NOT NULL, nom VARCHAR(255) NOT NULL, eleve_ideleve BIGINT NOT NULL, Note_idnote BIGINT NOT NULL, PRIMARY KEY (ideleve, eleve_ideleve, Note_idnote))")
    [EL Warning]: 2010-03-22 22:48:43.534--ServerSession(15020576)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: Table/View 'NOTE' existe déjà dans Schema 'TEST'.
    Error Code: 30000
    Call: CREATE TABLE note (idnote BIGINT NOT NULL, note VARCHAR(255), PRIMARY KEY (idnote))
    Query: DataModifyQuery(sql="CREATE TABLE note (idnote BIGINT NOT NULL, note VARCHAR(255), PRIMARY KEY (idnote))")
    [EL Warning]: 2010-03-22 22:48:43.69--ServerSession(15020576)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: Constraint 'ELEVE_NOTE_IDNOTE' existe déjà dans Schema 'TEST'.
    Error Code: 30000
    Call: ALTER TABLE eleve ADD CONSTRAINT eleve_Note_idnote FOREIGN KEY (Note_idnote) REFERENCES note (idnote)
    Query: DataModifyQuery(sql="ALTER TABLE eleve ADD CONSTRAINT eleve_Note_idnote FOREIGN KEY (Note_idnote) REFERENCES note (idnote)")
    [EL Warning]: 2010-03-22 22:48:43.814--ServerSession(15020576)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLException: Table/View 'SEQUENCE' existe déjà dans Schema 'TEST'.
    Error Code: 30000
    Call: CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))
    Query: DataModifyQuery(sql="CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))")
    [EL Warning]: 2010-03-22 22:48:44.212--UnitOfWork(9015524)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLIntegrityConstraintViolationException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
    Error Code: 20000
    Call: INSERT INTO eleve (ideleve, prenom, nom) VALUES (?, ?, ?)
            bind => [1, Dente, Ozzy]
    Query: InsertObjectQuery(testjpa.Eleve[id=1])
    Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLIntegrityConstraintViolationException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
    Error Code: 20000
    Call: INSERT INTO eleve (ideleve, prenom, nom) VALUES (?, ?, ?)
            bind => [1, Dente, Ozzy]
    Query: InsertObjectQuery(testjpa.Eleve[id=1])
            at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:699)
            at testjpa.Test.testPersistance(Test.java:61)
            at testjpa.Main.main(Main.java:21)
    Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLIntegrityConstraintViolationException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
    Error Code: 20000
    Call: INSERT INTO eleve (ideleve, prenom, nom) VALUES (?, ?, ?)
            bind => [1, Dente, Ozzy]
    Query: InsertObjectQuery(testjpa.Eleve[id=1])
            at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:800)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:866)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:586)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:529)
            at org.eclipse.persistence.internal.sessions.AbstractSession.executeCall(AbstractSession.java:914)
            at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:205)
            at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:191)
            at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:334)
            at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162)
            at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177)
            at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:461)
            at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
            at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
            at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:286)
            at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
            at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:675)
            at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:589)
            at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:109)
            at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:86)
            at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2863)
            at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1225)
            at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1207)
            at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1167)
            at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:197)
            at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(CommitManager.java:164)
            at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:116)
            at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:3260)
            at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1405)
            at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:547)
            at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithPreBuiltChangeSet(UnitOfWorkImpl.java:1551)
            at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.writeChanges(RepeatableWriteUnitOfWork.java:360)
            at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:696)
            ... 2 more
    Caused by: java.sql.SQLIntegrityConstraintViolationException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
            at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
            at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
            at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
            at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
            at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
            at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
            at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
            at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown Source)
            at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown Source)
            at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:791)
            ... 33 more
    Caused by: java.sql.SQLException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
            at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
            at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
            ... 43 more
    Caused by: ERROR 23502: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
            at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
            at org.apache.derby.impl.sql.execute.NormalizeResultSet.normalizeColumn(Unknown Source)
            at org.apache.derby.impl.sql.execute.NormalizeResultSet.normalizeRow(Unknown Source)
            at org.apache.derby.impl.sql.execute.NormalizeResultSet.getNextRowCore(Unknown Source)
            at org.apache.derby.impl.sql.execute.DMLWriteResultSet.getNextRowCore(Unknown Source)
            at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown Source)
            at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
            at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
            ... 37 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 7 seconds)

    le probleme principal :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLIntegrityConstraintViolationException: La colonne 'ELEVE_IDELEVE' ne peut pas accepter de valeur NULL.
    Error Code: 20000
    je ne sais pourquoi que j'ai cette erreur .

    merci

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    70
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 70
    Par défaut
    Essayez en supprimant la ligne 'eleve.setId(1L);' de ce que vous appelez "code de persistance"

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    506
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 506
    Par défaut
    merci pour ta réponse mais j'ai changé d'outils et de plateforme : trop prise de tête, je developpe sous eclipse avec eclipseLink.



    merci !

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

Discussions similaires

  1. Impossible de persist + flush plus de 1000 lignes
    Par taideen dans le forum Doctrine2
    Réponses: 13
    Dernier message: 26/02/2015, 08h19
  2. [2.x] Impossible de Persister une entité
    Par hx.jonathan dans le forum Symfony
    Réponses: 10
    Dernier message: 03/10/2011, 19h37
  3. Pb avec Hibernate : impossible de persister mon entité
    Par Atatorus dans le forum GWT et Vaadin
    Réponses: 5
    Dernier message: 18/02/2009, 00h39
  4. Création de persistence impossible car aucune PK
    Par jminguely dans le forum NetBeans
    Réponses: 6
    Dernier message: 22/02/2008, 10h35
  5. Réponses: 3
    Dernier message: 19/05/2006, 21h38

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