Bonjour à tous,
Je suis en train de développer une petite application Java et j'utilise le Framework Hibernate couplé avec le SGBD MySQL.
Manque de chance (ou de talent :s) j'ai la fameuse erreur:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: t_author is not mapped [SELECT author_id From t_author Where author_name='Jean Patie']
Je vous fais suivre le mapping et les .java pour un petit coup de main^^
Même si je comprend l'erreur je ne vois pas trop ce qu'Hibernate veut :s (probleme de casse ou autre)
Pour info, je ne veux pas utiliser createSQLQuery mais createQuery 
Merci d'avance
Classe Auteur
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
|
package auteur;
import java.util.HashSet;
import java.util.Set;
import publication.Publication;
import theme.Theme;
import etablissement.Etablissement;
public class Auteur {
private int id;
private String nom;
private String photo;
private Set<Auteur> listeAuteursThematiques= new HashSet<Auteur>();
private Set<Etablissement> listeEtablissementsPresents= new HashSet<Etablissement>();
private Set<Theme> listeTheme= new HashSet<Theme>();
private Set<Publication> listePublis= new HashSet<Publication>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public Set<Auteur> getListeAuteursThematiques() {
return listeAuteursThematiques;
}
public void setListeAuteursThematiques(Set<Auteur> listeAuteursThematiques) {
this.listeAuteursThematiques = listeAuteursThematiques;
}
public Set<Etablissement> getListeEtablissementsPresents() {
return listeEtablissementsPresents;
}
public void setListeEtablissementsPresents(
Set<Etablissement> listeEtablissementsPresents) {
this.listeEtablissementsPresents = listeEtablissementsPresents;
}
public Set<Theme> getListeTheme() {
return listeTheme;
}
public void setListeTheme(Set<Theme> listeTheme) {
this.listeTheme = listeTheme;
}
public Set<Publication> getListePublis() {
return listePublis;
}
public void setListePublis(Set<Publication> listePublis) {
this.listePublis = listePublis;
}
} |
Classe AuteurDAO
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
|
package dao;
import hibernate.HibernateInit;
import java.util.List;
import java.util.Vector;
import org.hibernate.Session;
import auteur.Auteur;
public class AuteurDAO {
Session session = HibernateInit.getSessionFactory().getCurrentSession();
public void creerAuteur(Auteur a){
session.getTransaction().begin();
session.save(a);
session.getTransaction().commit();
}
public void supprimerAuteur(Auteur a){
session.getTransaction().begin();
session.delete(a);
session.getTransaction().commit();
}
public void modifierAuteur(Auteur a){
session.getTransaction().begin();
session.saveOrUpdate(a);
session.getTransaction().commit();
}
@SuppressWarnings("unchecked")
public List<Auteur> consulterAuteur(String nom){
session.getTransaction().begin();
List<Auteur> list = session.createQuery("From T_author Where author_name='"+nom+"'").list();
session.getTransaction().commit();
return list;
}
@SuppressWarnings("unchecked")
public List<Auteur> consulterAuteurs(Vector<String> noms){
String r = "From T_author Where ";
for(int i=0; i<noms.size(); i++){
r+="author_name='"+noms.elementAt(i)+"' ";
if(i!=noms.size()-1){
r+="Or ";
}
}
session.getTransaction().begin();
List<Auteur> l = session.createQuery(r).list();
session.getTransaction().commit();
return l;
}
@SuppressWarnings("unchecked")
public List<Auteur> consulterTousAuteurs(){
session.getTransaction().begin();
List<Auteur> list = session.createQuery("From T_author").list();
session.getTransaction().commit();
return list;
}
@SuppressWarnings("unchecked")
public List<Integer> getIdAuteur(Auteur a){
session.getTransaction().begin();
List<Integer> list = session.createQuery("Select author_id From T_author Where author_name='"+a.getNom()+"'").list();
session.getTransaction().commit();
return list;
}
public boolean existeAuteur(Auteur a){
session.getTransaction().begin();
Integer IdAuteur = (Integer) session.createQuery("SELECT author_id From t_author Where author_name='"+a.getNom()+"'").uniqueResult();
session.getTransaction().commit();
return (IdAuteur != 0);
}
@SuppressWarnings("unchecked")
public int compterAuteurs() {
session.getTransaction().begin();
int l = (Integer) session.createQuery("Select count(*) From t_author").uniqueResult();
session.getTransaction().commit();
return l;
}
} |
Fichier Auteur.hbm.xml
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
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="auteur.Auteur" table="T_AUTHOR" lazy="false">
<id name="id" column="AUTHOR_ID">
</id>
<property name="nom" column="AUTHOR_NAME" />
<property name="photo" column="AUTHOR_PIC" />
<set name="listeAuteursThematiques" table="T_AUTHOR_AUTHOR" lazy="false">
<key column="AUTHOR_ID" />
<many-to-many class="auteur.Auteur" column="AUTHOR_ID_THEM" />
</set>
<set name="listeEtablissementsPresents" table="T_BUILDING_AUTHOR" lazy="false">
<key column="AUTHOR_ID" />
<many-to-many class="publication.Publication" column="BUILDING_ID" />
</set>
<set name="listeTheme" table="T_AUTHOR_THEME" lazy="false">
<key column="AUTHOR_ID" />
<many-to-many class="theme.Theme" column="THEME_ID" />
</set>
<set name="listePublis" table="T_PUBLI_AUTHOR" lazy="false">
<key column="AUTHOR_ID" />
<many-to-many class="publication.Publication" column="PUBLI_ID" />
</set>
</class>
</hibernate-mapping> |
Et l'hibernate.cfg.xml:
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
|
<?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>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///DOCNUM</property>
<property name="hibernate.connection.username">docnum</property>
<property name="hibernate.connection.password">docnum</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Drop and re-create the database schema on start-up, also try with update to keep the
previous values -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="auteur/Auteur.hbm.xml"/>
<mapping resource="etablissement/Etablissement.hbm.xml"/>
<mapping resource="motcle/MotCle.hbm.xml"/>
<mapping resource="publication/Publication.hbm.xml"/>
<mapping resource="theme/Theme.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Partager