Problème avec hibernate : IllegalArgumentException
Salut,
Alors que je suivais le cours de Serge Tahé JPA 5 (http://tahe.ftp-developpez.com/fichiers-archive/jpa.pdf), et dans un code que j'éssayais, l'erreur suivante est apparue :
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: entites.Personne
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:215)
at tests.InitDB.main(InitDB.java:31)
La structure de mon projet est la suivante :
+src
+entites
Personne.java
+tests
InitDB.java
main.java
META-INF
persistence.xml
L'erreur s'est produite lorsque j'ai essayé d'exécuter "InitDB.java", voici son code :
Code:
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
|
package tests;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import entites.Personne;
public class InitDB {
// constantes
private final static String TABLE_NAME = "jpa01_personne";
public static void main(String[] args) throws ParseException {
// récupérer un EntityManagerFactory à* partir de l'unité de persistance définie dans persistence.xml
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
// récupérer un EntityManager à* partir de l'EntityManagerFactory
EntityManager em = emf.createEntityManager();
// début transaction
EntityTransaction tx = em.getTransaction();
tx.begin();
// supprimer les éléments de la table PERSONNE
em.createNativeQuery("delete from " + TABLE_NAME).executeUpdate();
// mettre des éléments dans la table PERSONNE
Personne p1 = new Personne("Martin", "Paul", new SimpleDateFormat("dd/MM/yy").parse("31/01/2000"), true, 2);
Personne p2 = new Personne("Durant", "Sylvie", new SimpleDateFormat("dd/MM/yy").parse("05/07/2001"), false, 0);
// persistance des personnes
em.persist(p1);
em.persist(p2);
// affichage personnes
System.out.println("[personnes]");
for (Object p : em.createQuery("select p from Personne p order by p.nom asc").getResultList()) {
System.out.println(p);
}
// fin transaction
tx.commit();
// fin EntityManager
em.close();
// fin EntityManagerFactory
emf.close();
// log
System.out.println("terminé ...");
}
} |
Le résultat attendu dans le console est le suivant :
[Personne]
[2,0,Durant,Sylvie,05/07/2001,false,0]
[1,0,Martin,Paul,31/01/2000,true,2]
terminé...
Consernat le fichier persistence.xml voici son code :
Code:
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
|
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<!-- provider -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- Classes persistantes -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
<!-- logs SQL
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
-->
<!-- connexion JDBC -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa" />
<property name="hibernate.connection.username" value="jpa" />
<property name="hibernate.connection.password" value="jpa" />
<!-- création automatique du schéma -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<!-- Dialecte -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<!-- propriétés DataSource c3p0 -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
</properties>
</persistence-unit>
</persistence> |
Je tiens à signaler que les jars réquis dans pour le projet sont aussi ajoutés.
Merci pour votre aide