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

Hibernate Java Discussion :

Je n'obtiens pas de création auto de la BDD, pourquoi ?


Sujet :

Hibernate Java

  1. #1
    Membre confirmé
    Inscrit en
    Novembre 2006
    Messages
    167
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 167
    Par défaut Je n'obtiens pas de création auto de la BDD, pourquoi ?
    Bonjour,

    Alors voila j'essaie d'automatiquement créer les tables de ma BDD en fonction des POJOs et fichiers de mapping que j'ai.

    J'ai suivi le tutoriel officiel de NetBeans sur hibernate pour créer la configuration et la hiérarchie des fichiers nécessaire pour Hibernate.

    Je dispose simplement d'un fichier class (POJO) Person avec son fichier Person.hbm.xml et j'aimerais simplement que Hibernate me créé automatiquement la table derrière dans me BDD qui est pour le moment toujours vide.

    J'utilise Hibernate 3, JavaDB et Tomcat 6.

    Voici le code source des fichiers intéressants :

    hibernate.cfg.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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby://localhost:1527/ws</property>
        <property name="hibernate.connection.username">ws</property>
        <property name="hibernate.connection.password">ws</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping resource="travel/Person.hbm.xml"/>
      </session-factory>
    </hibernate-configuration>
    Person.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
     
    package travel;
    // Generated 11 juin 2009 09:44:25 by Hibernate Tools 3.2.1.GA
     
     
     
    /**
     * Person generated by hbm2java
     */
    public class Person  implements java.io.Serializable {
     
     
         private int personid;
         private String name;
         private String jobtitle;
     
     
        public Person() {
        }
     
     
        public Person(int personid) {
            this.personid = personid;
        }
        public Person(int personid, String name, String jobtitle) {
           this.personid = personid;
           this.name = name;
           this.jobtitle = jobtitle;
     
        }
     
        public int getPersonid() {
            return this.personid;
        }
     
        public void setPersonid(int personid) {
            this.personid = personid;
        }
        public String getName() {
            return this.name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getJobtitle() {
            return this.jobtitle;
        }
     
        public void setJobtitle(String jobtitle) {
            this.jobtitle = jobtitle;
        }
     
    }
    Person.hbm.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
     
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 11 juin 2009 09:44:35 by Hibernate Tools 3.2.1.GA -->
    <hibernate-mapping>
        <class name="travel.Person" table="PERSON" schema="TRAVEL">
            <id name="personid" type="int">
                <column name="PERSONID" />
                <generator class="assigned" />
            </id>
            <property name="name" type="string">
                <column name="NAME" length="50" />
            </property>
            <property name="jobtitle" type="string">
                <column name="JOBTITLE" length="50" />
            </property>
        </class>
    </hibernate-mapping>
    J'ai ensuite écrit une petite fonction d'ajout d'une Personne dans ma class SessionBean1.java car ma création de table ne se faisait toujours pas :

    SessionBean1.java (voir la méthode createPerson tout en bas)

    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
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package hibernatews;
     
    import com.sun.rave.web.ui.appbase.AbstractSessionBean;
    import javax.faces.FacesException;
    import org.hibernate.Session;
    import travel.NewHibernateUtil;
    import travel.Person;
     
    /**
     * <p>Session scope data bean for your application.  Create properties
     *  here to represent cached data that should be made available across
     *  multiple HTTP requests for an individual user.</p>
     *
     * <p>An instance of this class will be created for you automatically,
     * the first time your application evaluates a value binding expression
     * or method binding expression that references a managed bean using
     * this class.</p>
     *
     * @version SessionBean1.java
     * @version Created on 11 juin 2009, 11:51:56
     * @author andrew
     */
     
    public class SessionBean1 extends AbstractSessionBean {
        // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
     
        /**
         * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
         * This method is automatically generated, so any user-specified code inserted
         * here is subject to being replaced.</p>
         */
        private void _init() throws Exception {
        }
        // </editor-fold>
     
        /**
         * <p>Construct a new session data bean instance.</p>
         */
        public SessionBean1() {
        }
     
        /**
         * <p>This method is called when this bean is initially added to
         * session scope.  Typically, this occurs as a result of evaluating
         * a value binding or method binding expression, which utilizes the
         * managed bean facility to instantiate this bean and store it into
         * session scope.</p>
         * 
         * <p>You may customize this method to initialize and cache data values
         * or resources that are required for the lifetime of a particular
         * user session.</p>
         */
        @Override
        public void init() {
            // Perform initializations inherited from our superclass
            super.init();
            // Perform application initialization that must complete
            // *before* managed components are initialized
            // TODO - add your own initialiation code here
     
     
     
            // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
            // Initialize automatically managed components
            // *Note* - this logic should NOT be modified
            try {
                _init();
            } catch (Exception e) {
                log("SessionBean1 Initialization Failure", e);
                throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
            }
     
            // </editor-fold>
            // Perform application initialization that must complete
            // *after* managed components are initialized
            // TODO - add your own initialization code here
        }
     
        /**
         * <p>This method is called when the session containing it is about to be
         * passivated.  Typically, this occurs in a distributed servlet container
         * when the session is about to be transferred to a different
         * container instance, after which the <code>activate()</code> method
         * will be called to indicate that the transfer is complete.</p>
         * 
         * <p>You may customize this method to release references to session data
         * or resources that can not be serialized with the session itself.</p>
         */
        @Override
        public void passivate() {
        }
     
        /**
         * <p>This method is called when the session containing it was
         * reactivated.</p>
         * 
         * <p>You may customize this method to reacquire references to session
         * data or resources that could not be serialized with the
         * session itself.</p>
         */
        @Override
        public void activate() {
        }
     
        /**
         * <p>This method is called when this bean is removed from
         * session scope.  Typically, this occurs as a result of
         * the session timing out or being terminated by the application.</p>
         * 
         * <p>You may customize this method to clean up resources allocated
         * during the execution of the <code>init()</code> method, or
         * at any later time during the lifetime of the application.</p>
         */
        @Override
        public void destroy() {
        }
     
        /**
         * <p>Return a reference to the scoped data bean.</p>
         *
         * @return reference to the scoped data bean
         */
        protected ApplicationBean1 getApplicationBean1() {
            return (ApplicationBean1) getBean("ApplicationBean1");
        }
     
     
        public void createPerson(String name, String job) {
     
            Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
     
            session.beginTransaction();
     
            Person p = new Person();
            p.setName(name);
            p.setJobtitle(job);
     
            session.save(p);
     
            session.getTransaction().commit();
        }
     
    }
    J'avais du coup ajouté ça dans la classe Java de ma page 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
     
    @Override
        public void init() {
            // Perform initializations inherited from our superclass
            super.init();
            // Perform application initialization that must complete
            // *before* managed components are initialized
            // TODO - add your own initialiation code here
     
            // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
            // Initialize automatically managed components
            // *Note* - this logic should NOT be modified
            try {
                _init();
            } catch (Exception e) {
                log("Page1 Initialization Failure", e);
                throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
            }
     
            // </editor-fold>
            // Perform application initialization that must complete
            // *after* managed components are initialized
            // TODO - add your own initialization code here
            getSessionBean1().createPerson("Robert", "Informaticien");        <----- LIGNE AJOUTEE
        }
    Voila je pensais que simplement avec l'existance de Person.java et de son fichier mapping Person.hbm.xml je pouvais automatiquement créer la table derrière ? Quel est le problème chez moi ?

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    que fait cette méthode NewHibernateUtil.getSessionFactory() ?

Discussions similaires

  1. création auto d'un fichier texte
    Par gretch dans le forum Langage
    Réponses: 3
    Dernier message: 23/10/2006, 13h06
  2. probleme XPath, j'obtiens pas ma données ?
    Par Bruno13 dans le forum Langage
    Réponses: 7
    Dernier message: 02/02/2006, 14h25
  3. [CR9] pas de création nouvelle connexion
    Par CR9-Deb dans le forum Connectivité
    Réponses: 4
    Dernier message: 16/02/2005, 16h39

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