-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Where To Define My Named Queries?
PostPosted: Mon Jul 31, 2006 5:53 am 
Beginner
Beginner

Joined: Tue Jul 04, 2006 11:29 am
Posts: 20
Location: Cambs, UK
Hi all,

I'm having some problems with my named queries. According to what I have read, I believe I am doing this the right way but I'm having no luck.

I have two databases so I am defining config files to use when creating my session factories. Trouble is, when I use the second config file, none of the named queries seem to be found:

Code:
Exception in thread "main" org.hibernate.MappingException: Named query not known: getAllURLStore


I'll paste the code as it will make more sense.

Hibernate version: 3.1

Mapping File 1: hibernate.cfg.xml
Code:
<?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>
      <!-- The usual stuff here that you don't need to see -->
      <!-- e.g. database connections etc. -->
      
      <!-- Mapping files -->
      <mapping resource="com/oobjects/datastore/tables/Entry.hbm.xml"/>
      <mapping resource="com/oobjects/datastore/tables/Field.hbm.xml"/>
      <mapping resource="com/oobjects/datastore/tables/Value.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


Mapping File 2: config.cfg.xml
Code:
<?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>
      <!-- The usual stuff here that you don't need to see -->
      <!-- e.g. database connections etc. -->
   
      <!-- Mapping files -->
      <mapping resource="com/oobjects/datastore/tables/DataType.hbm.xml"/>
      <mapping resource="com/oobjects/datastore/tables/EntryCollection.hbm.xml"/>
      <mapping resource="com/oobjects/datastore/tables/HarvesterRule.hbm.xml"/>
      <mapping resource="com/oobjects/datastore/tables/URLStore.hbm.xml"/>
   </session-factory>   
</hibernate-configuration>


Here's one of my mapping files (from config.cfg.xml). As you can see there is a named query in it:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="com.oobjects.datastore.tables.URLStore" table="urlstore">
      <id name="url" column="url">
         <generator class="assigned"/>
      </id>
      <property name="collection"/>
   </class>
   
   <!--  Named Queries -->
   <query name="getAllURLStore">
      <![CDATA[from URLStore]]>
   </query>
</hibernate-mapping>


Finally, here is the code I am using to call my named query:
Code:
Session session = HibernateUtil.getSession(HibernateUtil.CONFIG_SESSION);
Transaction tx = session.beginTransaction();
List<URLStore> urls = session.getNamedQuery(HibernateUtil.NQ_GETALLURLSTORE).list();
tx.commit();
session.close();


Code from the HibernateUtil:
Code:
private static final String DATASTORE_HIBERNATE_CONFIG = "hibernate.cfg.xml";
private static final String CONFIGDB_HIBERNATE_CONFIG = "config.cfg.xml";
   
    static {
        try {
            // Create the SessionFactorys
            sessionFactory = new Configuration().configure(DATASTORE_HIBERNATE_CONFIG).buildSessionFactory();
            configSessionFactory = new Configuration().configure(CONFIGDB_HIBERNATE_CONFIG).buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            log.error("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
   
    public static Session getSession(int sessionType) {
       Session sess = null;
       SessionFactory sf = null;
       
       // Retrieve the correct SessionFactory
       switch(sessionType) {
          case CONFIG_SESSION:
             sf = configSessionFactory;
          case DATASTORE_SESSION:
          default:
             sf = sessionFactory;
       }
       
       // Get the current session or open a new one if an existing session does not exist
       try {
          sess = sf.getCurrentSession();
       } catch (HibernateException e) {
          sess = sf.openSession();
       }
       
      return sess;
    }


Sorry about all that code. So, can anyone see why the named queries aren't working when I am using the second config file (config.cfg.xml). I know the file is found OK and the database schema is created. It just can't seem to find the named queries.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 6:19 am 
Beginner
Beginner

Joined: Tue Jul 04, 2006 11:29 am
Posts: 20
Location: Cambs, UK
Never mind. I made a school boy mistake. Must read my code more thoroughly before posting.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.