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 :

[netbeans 6.5,vista] créer la BDD


Sujet :

JPA Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 959
    Par défaut [netbeans 6.5,vista] créer la BDD
    bonjour,

    je voudrais utiliser netbeans / JPA / Hibernate pour créer le base de données qui correspond à 2 fichiers de classes (employés et indemnités).
    les 2 classes sont reliées par une relation de 1 à plusieurs, je crois qu'il n'y a pas d'erreur de ce côté-là.

    1e question: jer voudrais savoir si en lançant le projet je peux créer les tables de la base de données.

    2e question: j'ai essayé de lancer cette création à partir de netbeans (clic-droit sur le fichier build.xml et option run target), mais j'obtiens des erreurs.

    voici maintenant divers fichiers où vous trouverez peut-être ce qui cloche:

    persistence.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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
      <persistence-unit name="intro_J2EEPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="hibernate.connection.username" value="jpa2"/>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.password" value="jpa2"/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa2"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
      </persistence-unit>
    </persistence>
    build.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
    40
    41
    42
    43
    44
    45
    46
    47
     
    <?xml version="1.0" encoding="UTF-8"?>
     
    <project name="intro_J2EE" default="BD" basedir=".">
       <import file="nbproject/build-impl.xml"/>
       <!-- nom du projet et version -->
       <property name="proj.name" value="jpa-hibernate" />
       <property name="proj.shortname" value="jpa-hibernate" />
       <property name="version" value="1.0" />
     
       <!-- Propriété globales -->
       <property name="src.java.dir" value="src" />
       <property name="dist.dir" value="dist" />
     
       <!-- le Classpath du projet -->
       <path id="project.classpath">
         <fileset dir="${dist.dir}">
           <include name="**/*.jar" />
         </fileset>
       </path>
     
       <!-- Hibernate Tools -->
       <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" 
    classpathref="project.classpath"/>
     
       <!-- Générer le schéma de la base -->
       <target name="DDL" description="Génération DDL base">
         <hibernatetool destdir="${basedir}">
           <!-- Utiliser META-INF/persistence.xml -->
           <jpaconfiguration />
           <!-- export -->
           <hbm2ddl drop="true" create="true" export="false" outputfilename="ddl/schema.sql" 
    delimiter=";" format="true" />
         </hibernatetool>
       </target>
     
       <!-- Générer la base -->
       <target name="BD" description="Génération BD">
         <hibernatetool destdir="${basedir}">
           <!-- Utiliser META-INF/persistence.xml -->
           <jpaconfiguration />
           <!-- export -->
           <hbm2ddl drop="true" create="true" export="true" outputfilename="ddl/schema.sql" 
    delimiter=";" format="true" />
         </hibernatetool>
       </target>
     </project>
    vous trouverez ci-jointe une photo de l'arborescence du projet.

    voici enfin l'erreur quand je lance la tâche BD:

    BD:
    Executing Hibernate Tool with a JPA Configuration
    1. task: hbm2ddl (Generates database schema)
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
    log4j:WARN Please initialize the log4j system properly.
    java.lang.NoClassDefFoundError: freemarker/template/SimpleHash
    at org.hibernate.tool.hbm2x.TemplateHelper.init(TemplateHelper.java:64)
    at org.hibernate.tool.hbm2x.AbstractExporter.setupTemplates(AbstractExporter.java:140)
    at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:93)
    at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
    at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    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:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:273)
    at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:499)
    at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
    Caused by: java.lang.ClassNotFoundException: freemarker.template.SimpleHash
    at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1400)
    at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1341)
    at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1088)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    ... 21 more
    BUILD FAILED (total time: 1 second)
    je précise que j'ai réussi à lancer un petit programme de test de log4j, je ne sais plus quoi faire.
    lolveley.

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Bah il te manque le jar de la librairie freemarker dans ton classpath.

  3. #3
    Membre éprouvé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 959
    Par défaut
    merci pour ta réponse, sinok.
    j'ai importé freemarker 2.3.x, et je vais faire des tests.

    lolveley.

  4. #4
    Membre éprouvé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 959
    Par défaut
    eh bien j' obtiens une nouvelle erreur:
    BD:
    Executing Hibernate Tool with a JPA Configuration
    1. task: hbm2ddl (Generates database schema)
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
    log4j:WARN Please initialize the log4j system properly.
    An exception occurred while running exporter #2:hbm2ddl (Generates database schema)
    To get the full stack trace run ant with -verbose
    org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(employés)]
    org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(employés)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
    at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:138)
    at org.hibernate.mapping.Column.getSqlType(Column.java:182)
    at org.hibernate.mapping.Table.sqlCreateString(Table.java:394)
    at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:848)
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:94)
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:61)
    at org.hibernate.tool.hbm2x.Hbm2DDLExporter.doStart(Hbm2DDLExporter.java:79)
    at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:95)
    at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
    at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at sun.reflect.GeneratedMethodAccessor99.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:273)
    at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:499)
    at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
    BUILD FAILED (total time: 0 seconds)
    je crois qu'il n'aime pas le fait que ma relation n'est définie que dans un sens, pourtant c'est ce qui est demandé par le tutoriel.

    voici le code des classes "persistées":

    Employés.java
    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package jpa;
     
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    import javax.persistence.Version;
     
    /**
     *
     * @author lolveley
     */
     
    @Entity
    @Table(name="employes")
    public class Employés implements Serializable{
     
        public Employés(int ss, String nom, String prénom, int code_postal, String adresse, String ville, jpa.indemnités indice) {
            this.ss = ss;
            this.nom = nom;
            this.prénom = prénom;
            this.code_postal = code_postal;
            this.adresse = adresse;
            this.ville = ville;
            this.indice = indice;
        }
     
        public Employés() {
        }
     
        @Id
        @GeneratedValue(strategy =GenerationType.AUTO)
        private int ss;
     
        //@SuppressWarnings(unused)
        @Version
        private int version;
     
        @Column(length=25,nullable=false)
        private String nom;
     
        @Column(name="prenom",length=25,nullable=false)
        private String prénom;
     
        @Column(nullable=false)
        private int code_postal;
     
        @Column(length=25,nullable=false)
        private String adresse;
     
        @Column(length=25,nullable=false)
        private String ville;
     
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="indemnites_indice")
        private indemnités indice;
     
     
        //***************************************************
     
        public int getVersion() {
            return version;
        }
     
        public void setVersion(int version) {
            this.version = version;
        }
     
        public String getAdresse() {
            return adresse;
        }
     
        public void setAdresse(String adresse) {
            this.adresse = adresse;
        }
     
        public int getCode_postal() {
            return code_postal;
        }
     
        public void setCode_postal(int code_postal) {
            this.code_postal = code_postal;
        }
     
        public jpa.indemnités getIndice() {
            return indice;
        }
     
        public void setIndice(jpa.indemnités indice) {
            this.indice = indice;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public String getPrénom() {
            return prénom;
        }
     
        public void setPrénom(String prénom) {
            this.prénom = prénom;
        }
     
        public int getSs() {
            return ss;
        }
     
        public void setSs(int ss) {
            this.ss = ss;
        }
     
        public String getVille() {
            return ville;
        }
     
        public void setVille(String ville) {
            this.ville = ville;
        }
     
     
        @Override
        public String toString (){
            return ("Employé : "+this.prénom+" "+this.nom+", version :"+this.getVersion());
     
     
        }
     
     
    }
    et indemnités.java

    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package jpa;
     
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Version;
    /**
     *
     * @author lolveley
     */
     
    @Entity
    @Table(name="indemnites")
    public class indemnités implements Serializable{
     
        @Id
        @GeneratedValue(strategy =GenerationType.AUTO)
        private int indice;
     
        @Version
        private int version;
     
        @Column(nullable=false)
        private float base_heure;
     
        @Column(nullable=false)
        private float entretien_jour;
     
        @Column(nullable=false)
        private float repas_jour;
     
        @Column(nullable=false)
        private float indemnités_cp;
     
     
        private Set<Employés> employés = new HashSet<Employés>();
     
     
     
     
        //**************************************************
     
        public Set<Employés> getEmployés() {
            return employés;
        }
     
        public void setEmployés(Set<Employés> employés) {
            this.employés = employés;
        }
        public Set<Employés> AddEmployé(Employés ze_employé){
     
            employés.add(ze_employé);
            return employés;
     
        }
     
        public Set<Employés> RemoveEmployés (Employés ze_employé) throws Exception{
     
            try {
                employés.remove(ze_employé);
     
            }
            catch (Exception e){
                System.out.println("ERREUR lors d'une suppression d'employés dans 'indemnités'");
                throw (e);
            }
            return employés;
     
        }
     
     
       public int getVersion() {
            return version;
        }
     
        public void setVersion(int version) {
            this.version = version;
        }
     
        public float getBase_heure() {
            return base_heure;
        }
     
        public void setBase_heure(float base_heure) {
            this.base_heure = base_heure;
        }
     
        public float getEntretien_jour() {
            return entretien_jour;
        }
     
        public void setEntretien_jour(float entretien_jour) {
            this.entretien_jour = entretien_jour;
        }
     
        public float getIndemnités_cp() {
            return indemnités_cp;
        }
     
        public void setIndemnités_cp(float indemnites_cp) {
            this.indemnités_cp = indemnites_cp;
        }
     
        public int getIndice() {
            return indice;
        }
     
        public void setIndice(int indice) {
            this.indice = indice;
        }
     
        public float getRepas_jour() {
            return repas_jour;
        }
     
        public void setRepas_jour(float repas_jour) {
            this.repas_jour = repas_jour;
        }
     
        public void persist(Object object) {
            EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("intro_J2EEPU");
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            try {
                em.persist(object);
                em.getTransaction().commit();
            } catch (Exception e) {
                e.printStackTrace();
                em.getTransaction().rollback();
            } finally {
                em.close();
            }
        }
     
        @Override
        public String toString (){
     
            return ("Indemnités :"+this.indice+", version :"+this.version);
     
        }
     
    }
    lolveley.

  5. #5
    Membre éprouvé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2006
    Messages
    959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2006
    Messages : 959
    Par défaut
    j'ai réussi!!!
    j'ai implanté l'annotation @OneToMany, et j'ai lancé la tâche BD (dans netbeans elle est accessible via le fichier build.xml).
    Y-a-t'il une façon de lancer la tâche automatiquement, à chaque exécution de l'appli?(j'ai modifié la tâche par défaut dans build.xml mais qd je lance le programme la BDD n'est pas remplie).

    enfin, le principal est fait!

    lolveley.

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

Discussions similaires

  1. créer une BDD sur vista
    Par Tackichi dans le forum Débuter
    Réponses: 4
    Dernier message: 28/05/2010, 09h40
  2. [Recherche] Tuto netbean 5.5, EJB,Jboss et BDD
    Par coye dans le forum NetBeans
    Réponses: 1
    Dernier message: 08/12/2006, 11h01
  3. [D7] [MySQL] comment créer 1 BDD à partir d'1 appli delphi7
    Par raoulmania dans le forum Bases de données
    Réponses: 7
    Dernier message: 16/11/2005, 19h26
  4. comment puis-je créer un BDD en fichier texte
    Par slyder dans le forum Bases de données
    Réponses: 2
    Dernier message: 19/01/2005, 11h05
  5. Créer une BDD Oracle sur windows
    Par calimero82 dans le forum Administration
    Réponses: 7
    Dernier message: 25/10/2004, 16h30

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