-->
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.  [ 6 posts ] 
Author Message
 Post subject: Exception while subselect fetching (bug?)
PostPosted: Wed May 11, 2005 1:05 pm 
Newbie

Joined: Wed Jan 05, 2005 6:54 am
Posts: 15
Hi,

i just tried the subselect feature. It seems to work fine is some cases, in others not.

Trying to load a "Category" with "parent" (also of type Category) == null produces the following subselect for a set of children entities (of some other type):

select category0_.CATEGORY_ID from CORE_CATEGORY category0_ where category0_.PARENT_ID is null

The statement, of course, works.

But if the Category has a parent, the following subselect is used for subselect fetching:

select category0_.CATEGORY_ID from CORE_CATEGORY category0_ where category0_.PARENT_ID = ?

This does not work. I get the following SQLGrammarException:

Code:
Caused by: org.hibernate.exception.SQLGrammarException: could not load collection by subselect: [de.w3solutions.dms.persistent.core.Category.attributes#<40, 42>]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:1478)
   at org.hibernate.loader.collection.SubselectCollectionLoader.initialize(SubselectCollectionLoader.java:55)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:483)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1422)
   at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:271)
   at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:215)
   at org.hibernate.loader.Loader.doList(Loader.java:1562)
   at org.hibernate.loader.Loader.list(Loader.java:1545)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:840)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at de.w3solutions.dms.database.hibernate.CategoryDAO.loadChildCategories(CategoryDAO.java:63)
   ... 83 more
Caused by: java.sql.SQLException: Parameter #1 has not been set.
   at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareSQL(ConnectionJDBC2.java:503)
   at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:666)
   at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:296)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:118)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1239)
   at org.hibernate.loader.Loader.doQuery(Loader.java:374)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
   at org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:1473)
   ... 97 more


I tried to figure out the reasons: The parameter list is, in deed, empty. Somehow the SubselectCollectionLoader gets an empty query parameter list on creation. In the hibernate code (org.hibernate.loader.Loader.doQuery(Loader.java:373)) it seems as if the collection keys are never bound to the prepared statement.

Or am I missing something?

Thx & bye.

Pvblivs

p.s.: my hibernate version is 3.0.3


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 2:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
How can we help you if you don't show any Hibernate code?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 6:04 am 
Newbie

Joined: Wed Jan 05, 2005 6:54 am
Posts: 15
Hi gavin,

I wrote some simple test code and it produces the same error:

Mapping file

Code:
<class name="test.TestEntity" table="TEST_ENTITY">

      <id name="id" type="long">
         <column name="ENTITY_ID" not-null="true" />
         <generator class="native" />
      </id>
      
      <property name="text" type="string">
         <column name="TEXT"  not-null="true"/>
      </property>

      <many-to-one name="parent" class="test.TestEntity" column="PARENT_ID" />

      <set name="properties" table="TEST_ENTITY_PROPERTY" lazy="false" fetch="subselect" cascade="all-delete-orphan">
         <key column="ENTITY_ID" />
         <many-to-many class="test.TestProperty" column="PROPERTY_ID" />
      </set>

   </class>

   <class name="test.TestProperty" table="TEST_PROPERTY">

      <id name="id" type="long">
         <column name="ENTITY_ID" not-null="true" />
         <generator class="native" />
      </id>
      
      <property name="text" type="string">
         <column name="TEXT"  not-null="true"/>
      </property>

   </class>


I don't post the pojos here as they are as simple as the mappings. Here is my test class:

HibernateTest

Code:
package test;

import java.util.List;

import junit.framework.TestCase;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import de.w3solutions.dms.database.hibernate.HibernateDatabase;
import de.w3solutions.dms.database.hibernate.HibernateSessionFactoryDatabase;

public class HibernateTest extends TestCase {

   private HibernateDatabase   database;

   private TestEntity            parent;

   protected void setUp() {
      log("*** Performing Setup");
      Transaction tx = null;
      try {
         database = new HibernateSessionFactoryDatabase(new Configuration().configure());
         Session session = database.getSession();
         log("Cleaning up");
         tx = session.beginTransaction();
         Query q = session.createQuery("from TestEntity");
         List l = q.list();
         for (int i = 0; i < l.size(); i++)
            database.delete(l.get(i));
         tx.commit();
         log("Creating test data");
         parent = new TestEntity(null, "Parent");
         parent.addProperty("some property a");
         TestEntity child1 = new TestEntity(parent, "Child 1");
         child1.addProperty("some property c");
         TestEntity child2 = new TestEntity(parent, "Child 2");
         child2.addProperty("some property d");
         TestEntity parent2 = new TestEntity(null, "Some other Parent");
         parent2.addProperty("some property b");
         log("Saving test data");
         tx = session.beginTransaction();
         database.save(parent);
         database.save(parent2);
         database.save(child1);
         database.save(child2);
         tx.commit();
      } catch (RuntimeException e) {
         e.printStackTrace();
         rollback(tx);
         throw e;
      } finally {
         database.close();
      }
      log("*** Setup performed");
   }

   public void testWithoutParent() {
      log("Testing without parent");
      Transaction tx = null;
      try {
         Session session = database.getSession();
         tx = session.beginTransaction();
         Query q = session.createQuery("from TestEntity as te where te.parent is null");
         List l = q.list();
         assertTrue(l.size() == 2);
         tx.commit();
      } catch (RuntimeException e) {
         e.printStackTrace();
         rollback(tx);
         throw e;
      } finally {
         database.close();
      }
   }

   public void testWithParent() {
      log("Testing with parent");
      Transaction tx = null;
      try {
         Session session = database.getSession();
         tx = session.beginTransaction();
         Query q = session.createQuery("from TestEntity as te where te.parent = :parent");
         q.setEntity("parent", parent);
         List l = q.list();
         tx.commit();
      } catch (RuntimeException e) {
         e.printStackTrace();
         rollback(tx);
         throw e;
      } finally {
         database.close();
      }
   }

   protected void tearDown() throws Exception {
      log("*** Performing teardown");
      Transaction tx = null;
      try {
         Session session = database.getSession();
         // clean up
         tx = session.beginTransaction();
         Query q = session.createQuery("from TestEntity");
         List l = q.list();
         for (int i = 0; i < l.size(); i++)
            database.delete(l.get(i));
         tx.commit();
      } catch (RuntimeException e) {
         e.printStackTrace();
         rollback(tx);
         throw e;
      } finally {
         database.close();
      }
      log("*** Teardown performed");
   }

   private void log(String string) {
      System.out.println(string);
   }

   private void logError(String string) {
      System.err.println(string);
   }

   private void rollback(Transaction tx) {
      try {
         tx.rollback();
      } catch (HibernateException e) {
      }
      logError("*** !!! ROLLBACK !!! ***");
   }

}


And finally the log:

Code:
*** Performing Setup
2005-05-12 10:59:15,671 [main] WARN  net.sf.ehcache.config.Configurator - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Dokumente%20und%20Einstellungen/Stefan%20Schubert/Eigene%20Dateien/dev/w3dms/lib/persistent/ehcache-1.1.jar!/ehcache-failsafe.xml
Cleaning up
Creating test data
Saving test data
*** Setup performed
Testing without parent
*** Performing teardown
*** Teardown performed
*** Performing Setup
Cleaning up
Creating test data
Saving test data
*** Setup performed
Testing with parent
2005-05-12 10:59:17,687 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Parameter #1 has not been set.
org.hibernate.exception.SQLGrammarException: could not load collection by subselect: [test.TestEntity.properties#<187, 188>]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:1478)
   at org.hibernate.loader.collection.SubselectCollectionLoader.initialize(SubselectCollectionLoader.java:55)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:483)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1422)
   at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:271)
   at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:215)
   at org.hibernate.loader.Loader.doList(Loader.java:1562)
   at org.hibernate.loader.Loader.list(Loader.java:1545)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:840)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at test.HibernateTest.testWithParent(HibernateTest.java:88)
   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:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Caused by: java.sql.SQLException: Parameter #1 has not been set.
   at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareSQL(ConnectionJDBC2.java:503)
   at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:666)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:118)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1239)
   at org.hibernate.loader.Loader.doQuery(Loader.java:374)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
   at org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:1473)
   ... 29 more
*** !!! ROLLBACK !!! ***
*** Performing teardown
*** Teardown performed


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 7:11 am 
Newbie

Joined: Wed Jan 05, 2005 6:54 am
Posts: 15
Hi again,

was my test code of any use for you? To return to my question: Is this a bug or some fault of me?

Regards,

Pvblivs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2005 5:41 am 
Newbie

Joined: Wed Jul 13, 2005 5:36 am
Posts: 7
I have the same problem (I'm using Hibernate 3.0.5) and it appears the query is not generated correctly. I'm using MySQL 4.1 which does support subselect (I have tested them at the command line) - have you found a solution?

_________________
--
Costin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 10:21 am 
Beginner
Beginner

Joined: Tue Apr 12, 2005 9:15 pm
Posts: 24
I am having the same problem. Is this a hibernate bug or is there a solution to this issue?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.