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 :

Error java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Mai 2003
    Messages
    351
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 351
    Par défaut Error java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException
    Bonjour, j'ai un souci lors de l'exécution d'une requête dans une forme.

    Fichier Dao
    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
     
    @Transactional
    public class Dao implements IDao{
        @PersistenceContext
    	private EntityManager em;
     
       public Embauche getEmbauche(String nom){ //,Date date){
            //tx.begin();
            //Query q = em.createQuery("select e from Embauche e where e.datedeb <= :date between and e.datefin >= :date join e.embauche p where p.nom||' 'p.prenom like :nom ");
            //Query q = em.createQuery("select e from Embauche e join e.embauche p where p.nom||' '||p.prenom like :nom ");
            Query q = em.createQuery("select e from Embauche e join e.embauche p where p.nom = :nom ").setParameter("nom", nom);
            //q.setParameter("date", date);
            return (Embauche)q.getSingleResult();
        }
    }
    Fichier service
    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
     
    @Transactional
    public class Service implements IService{
     // couche [dao]
    	private IDao dao;
     
        public IDao getDao() {
    		return dao;
    	}
     
    	public void setDao(IDao dao) {
    		this.dao = dao;
    	}
    public Embauche getEmbauche(String nom){
          return dao.getEmbauche(nom;
      }
    }
    Forme de test
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    public class FrmEmbauche extends javax.swing.JPanel {
     private void executeHQLQuery(){
     
                // configuration de l'application
    		    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    		    // couche service
    		    service = (IService) ctx.getBean("service");
     
            Embauche e = service.getEmbauche( p_nom.getText().trim());//,new SimpleDateFormat("dd/MM/yyyy").parse(p_date.getText().trim()));
                System.out.println(e.toString());
     
     
        }
    Et lors de l'exécution j'ai l'erreur
    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Embauche is not mapped [select e from Embauche e join e.embauche p where p.nom = :nom ]
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:634)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:95)
    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:585)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:180)
    at $Proxy5.createQuery(Unknown Source)
    at mis.cinq.dao.Dao.getEmbauche(Dao.java:58)
    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:585)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:203)
    at $Proxy8.getEmbauche(Unknown Source)
    at mis.cinq.service.Service.getEmbauche(Service.java:45)
    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:585)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:203)
    at $Proxy9.getEmbauche(Unknown Source)
    at mis.cinq.view.FrmEmbauche.executeHQLQuery(FrmEmbauche.java:463)
    at mis.cinq.view.FrmEmbauche.runQuery(FrmEmbauche.java:453)
    at mis.cinq.view.FrmEmbauche.BExecuterActionPerformed(FrmEmbauche.java:448)
    at mis.cinq.view.FrmEmbauche.access$000(FrmEmbauche.java:30)
    at mis.cinq.view.FrmEmbauche$1.actionPerformed(FrmEmbauche.java:397)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:5517)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
    at java.awt.Component.processEvent(Component.java:5282)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.awt.Component.dispatchEventImpl(Component.java:3984)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.awt.Component.dispatchEvent(Component.java:3819)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.java:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1791)
    at java.awt.Component.dispatchEvent(Component.java:3819)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Caused by: org.hibernate.hql.ast.QuerySyntaxException: Embauche is not mapped [select e from Embauche e join e.embauche p where p.nom = :nom ]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
    ... 59 more
    BUILD STOPPED (total time: 30 seconds)
    j'ai besoin de votre aide

  2. #2
    Membre Expert Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Par défaut
    D'après a trace d'erreur tu n'as pas mappé la classe Embauche. Tu as bien créé un fichier de mapping pour cette classe? Tu l'as bien ajouté à ta config hibernate?

  3. #3
    Membre éclairé
    Inscrit en
    Mai 2003
    Messages
    351
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 351
    Par défaut réponse
    Oui je crois l'avoir mappé.
    voilà ma procédure
    j'ai d'abord créer la classe Embauche

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    @SuppressWarnings( { "unused", "serial" })
    @Entity
    @Table(name = "Embauche")
     
    /**
     *
     * @author laplace
     */
    public class Embauche implements Serializable{
    }
    Puis j'ai généré la base de donnée sous MYSQL5 avec le 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
    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/jpan" />
    			<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>
    Par la suite pour accéder à la base j'ai modifier persistence.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <?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="jpan" transaction-type="RESOURCE_LOCAL" />
    </persistence>
    et aussi avec spring-config.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
    48
    49
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     
    	<!-- couches applicatives -->
    	<bean id="dao" class="mis.cinq.dao.Dao" />
    	<bean id="service" class="mis.cinq.service.Service">
    		<property name="dao" ref="dao" />
    	</bean>
     
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<!-- 
    					<property name="showSql" value="true" />
    				-->
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    		</property>
    	</bean>
     
    	<!-- la source de donnéees DBCP -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/jpan" />
    		<property name="username" value="jpa" />
    		<property name="password" value="jpa" />
    	</bean>
     
    	<!-- le gestionnaire de transactions -->
    	<tx:annotation-driven transaction-manager="txManager" />
    	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
     
    	<!-- traduction des exceptions -->
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     
    	<!-- persistence -->
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
     
    </beans>
    ce sont ces 2 fichiers que j'utilise avec la couche service pour me connecter
    Ce code est dans le panel pour me connecter et exécuter ma requete
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     // configuration de l'application
    		    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    		    // couche service
    		    service = (IService) ctx.getBean("service");
    Embauche e = service.getEmbauche( p_nom.getText().trim(),new SimpleDateFormat("dd/MM/yyyy").parse(p_date.getText().trim()));
                System.out.println(e.toString());
    Dao
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     public Embauche getEmbauche(String nom,Date date){
            //tx.begin();
            //Query q = em.createQuery("select e from Embauche e where e.datedeb <= :date between and e.datefin >= :date join e.embauche p where p.nom||' 'p.prenom like :nom ");
            //Query q = em.createQuery("select e from Embauche e join e.embauche p where p.nom||' '||p.prenom like :nom ");
            Query q = em.createQuery("select e from Embauche e join e.embauche p where p.nom = :nom ");
            q.setParameter("nom", nom);
            q.setParameter("date", date);
            return (Embauche)q.getSingleResult();
        }

  4. #4
    Membre Expert Avatar de Fench
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Mai 2002
    Messages
    2 353
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Groenland

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 353
    Par défaut
    Salut,

    D'après ton code, c impossible de mapper q car

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            q.setParameter("date", date);
    ya pas :date

  5. #5
    Membre éclairé
    Inscrit en
    Mai 2003
    Messages
    351
    Détails du profil
    Informations forums :
    Inscription : Mai 2003
    Messages : 351
    Par défaut
    Je ne comprend pas je ne fais que suivre la doc pour un passage de paramètre.
    Comment faut il donc procéder

  6. #6
    Membre Expert Avatar de Fench
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Mai 2002
    Messages
    2 353
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Groenland

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 353
    Par défaut
    Le pb est que tu ta query telle que:
    Query q = em.createQuery("select e from Embauche e join e.embauche p where p.nom = :nom ");
    Et je prends le dernier essai

    OR dedans tu n'utilises que le paramétre :nom ET DONC pas :date

    ALORS tu vires le bout de code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            q.setParameter("date", date);
    ET je pense qu'il pourra bien faire le mapping de ta query

Discussions similaires

  1. Réponses: 2
    Dernier message: 03/05/2010, 21h13
  2. Réponses: 0
    Dernier message: 13/10/2008, 17h39
  3. Réponses: 7
    Dernier message: 01/10/2008, 10h48
  4. Réponses: 2
    Dernier message: 21/07/2008, 14h53
  5. org.hibernate.hql.ast.QuerySyntaxError: unexpected token
    Par oughlad dans le forum Hibernate
    Réponses: 9
    Dernier message: 26/05/2006, 14h20

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