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 :

hibernate et mysql


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 8
    Par défaut hibernate et mysql
    saluts à tous.
    merci de m'aider sur cette erreur.

    %%%% Error Creating SessionFactory %%%%
    org.hibernate.InstantiationException: could not instantiate test objectEmployee
    at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:25)
    at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:41)
    at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:104)
    at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:398)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:199)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1043)
    at business.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:51)
    at business.LibraryTest.setUp(LibraryTest.java:68)
    at business.LibraryTest.main(LibraryTest.java:24)
    java.lang.NullPointerException
    at business.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:58)
    at business.LibraryTest.setUp(LibraryTest.java:68)
    at business.LibraryTest.main(LibraryTest.java:24)


    mes fichiers sont:
    hibernate.cfg.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>

    <session-factory >
    <!-- local connection properties -->
    <property name="hibernate.connection.url">jdbc:mysql://localhost/sampledb</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <!-- property name="hibernate.connection.pool_size"></property -->
    <!-- dialect for MySQL -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <mapping resource="business/Employee.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

    Employee.hbm.xml:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

    <hibernate-mapping>
    <class name="Employee" table="employee" schema="public">
    <id name="id" type="java.lang.Integer" column="id">
    <generator class="increment" />
    </id>

    <property name="firstname" type="java.lang.String">
    <column name="firstname" />
    </property>
    <property name="lastname" type="java.lang.String">
    <column name="lastname" />
    </property>


    </class>
    </hibernate-mapping>

    Test:


    package business;

    import java.util.Iterator;
    import java.util.List;

    import org.apache.log4j.Logger;
    import org.hibernate.Session;
    import org.hibernate.Transaction;


    public class LibraryTest {
    private Session session;

    private Logger log;

    public static void main(String[] args) {
    /*
    * hibernate needs log4j. Either create a log4j.properties file in the
    * source directory * or alternatively make the following to create a
    * standard configuration BasicConfigurator.configure();
    */
    LibraryTest libraryTest = new LibraryTest();
    try {
    libraryTest.setUp();
    libraryTest.testCreateDomains();
    libraryTest.listEmployees();
    libraryTest.tearDown();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    private void testCreateDomains() {
    Transaction tx = session.beginTransaction();
    Employee emp = new Employee();
    emp.setId(4);
    emp.setFirstname("Hatem");
    emp.setLastname("SFAXI");
    session.save(emp);
    tx.commit();

    }




    protected void setUp() throws Exception {
    session = HibernateSessionFactory.currentSession();
    log = Logger.getLogger(this.getClass());
    }

    protected void tearDown() throws Exception {
    HibernateSessionFactory.closeSession();
    }


    private void listEmployees() {
    log.info("####### list employees");

    Transaction tx = session.beginTransaction();
    List employees = session.createQuery("select c from Employee as c")
    .list();
    for (Iterator iter = employees.iterator(); iter.hasNext() {
    Employee element = (Employee) iter.next();
    //Set books = element.getBooks();
    System.out.println(element);
    log.info(element);
    }
    tx.commit();

    }

    /**
    * @return Returns the session.
    */
    public Session getSession() {
    return session;
    }

    /**
    * @param session
    * The session to set.
    */
    public void setSession(Session session) {
    this.session = session;
    }
    }

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    383
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 383
    Par défaut
    Il faut que ta classe étende junit.framework.TestCase.

    Il y a un article qui explique comment faire un test JUnit pour Hibernate :
    http://www.theserverside.com/tt/arti...?l=UnitTesting

Discussions similaires

  1. Problème avec hibernate et MySQL!
    Par sofien dans le forum Outils
    Réponses: 5
    Dernier message: 27/08/2008, 09h06
  2. essai "debuter avec hibernate" avec MYSQL et j'y arrive pas
    Par alligator424 dans le forum Hibernate
    Réponses: 5
    Dernier message: 08/08/2007, 14h02
  3. [Mapping] Hibernate et MySQL
    Par dvicente72 dans le forum Hibernate
    Réponses: 11
    Dernier message: 06/08/2007, 16h24
  4. Probleme Hibernate 3 + MySQL 4.1.2
    Par herbert dans le forum SQL Procédural
    Réponses: 7
    Dernier message: 10/07/2007, 12h01
  5. Probleme Hibernate 3 + MySQL 4.1.2
    Par herbert dans le forum Hibernate
    Réponses: 5
    Dernier message: 05/09/2006, 03h36

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