[hibernate-issues] [Hibernate-JIRA] Created: (HHH-3920) <list> in component gives ArrayIndexOutOfBoundsException when querying

Elia Gaglio (JIRA) noreply at atlassian.com
Wed May 20 10:19:13 EDT 2009


<list> in component gives ArrayIndexOutOfBoundsException when querying 
-----------------------------------------------------------------------

                 Key: HHH-3920
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3920
             Project: Hibernate Core
          Issue Type: Bug
          Components: query-hql
    Affects Versions: 3.3.x
         Environment: Last Hibernate version, DB2 9
            Reporter: Elia Gaglio
            Priority: Minor
         Attachments: testJava.zip

SITUATION:

I have a <class> Holder which contains a <component> class Container1 which, in turn, has a List of objects of <class> Container2.

---

CLASS DEFINITIONS:

This is Holder:

package testPZ;

import java.io.Serializable;

public class Holder implements Serializable{
   private static final long serialVersionUID = 1L;
   String oid;
   Container1 con1;
   
   public Holder(){
      
   }
   
   public Holder(String _oid){
      oid = _oid;
   }

   public Container1 getCon1() {
      return con1;
   }

   public void setCon1(Container1 con1) {
      this.con1 = con1;
   }

   public String getOid() {
      return oid;
   }

   public void setOid(String oid) {
      this.oid = oid;
   }
}


This is Container1:

package testPZ;

import java.io.Serializable;
import java.util.*;

public class Container1 implements Serializable{
   private static final long serialVersionUID = 1L;
   List listCon2 = new ArrayList();
   
   public Container1(){
      
   }

   public List getListCon2() {
      return listCon2;
   }

   public void setListCon2(List listCon2) {
      this.listCon2 = listCon2;
   }
}


And this is Container2:

package testPZ;

import java.io.Serializable;

public class Container2 implements Serializable{
   private static final long serialVersionUID = 1L;
   String oid;
   
   public Container2(){
      
   }
   
   public Container2(String _oid){
      oid = _oid;
   }

   public String getOid() {
      return oid;
   }

   public void setOid(String oid) {
      this.oid = oid;
   }
}


---

XML FILES:

This is the hibernate.cfg.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="connection.username">user</property>
		<property name="connection.password">password</property>
		<property name="connection.url">
			jdbc:db2://localhost:50000/DB
		</property>
		<property name="connection.driver_class">
			com.ibm.db2.jcc.DB2Driver
		</property>
		<property name="dialect">
			org.hibernate.dialect.DB2Dialect
		</property>
		<property name="hibernate.jdbc.batch_size">
			0
		</property>
		<property name="hibernate.cglib.use_reflection_optimizer">
			false;
		</property>
		<property name = "show_sql">
			false
		</property>
		<property name = "hibernate.format_sql">
			false
		</property>
		<property name="hibernate.cache.use_second_level_cache">false</property>			
	</session-factory>
</hibernate-configuration>


As you can see, Holder is mapped with a <class> tag, Container1 as a <component> which in turn contains a <list> of <class> Container2. Here is the complete XML file mapping.xml:

<?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 auto-import="false" default-cascade="none">
	<!--table="Holder"-->
	<class name="testPZ.Holder" table="Holder">
		<composite-id>
			<key-property name="oid" type="string">
				<column name="oid" sql-type="char(35)" />
			</key-property>
		</composite-id>
		<component name="con1">
			<list lazy="false" fetch="subselect" name="listCon2">
				<cache usage="read-write" />
				<key>
					<!--column name=""/-->
					<column name="fk_holder" index="i_con2"
						sql-type="char(35)" />
				</key>
				<!--index column="si_accountHolders_accountData"/-->
				<index column="si_con2" />
				<one-to-many class="testPZ.Container2" />
			</list>
		</component>
	</class>
	<!--table="TAccountHolder"-->
	<class name="testPZ.Container2" table="Container2">
		<id name="oid" type="string">
			<column name="oid" sql-type="char(35)" />
		</id>
	</class>
</hibernate-mapping>


---

TESTS:

First of all I create the db schema with the following code snippet:

Configuration cfg = new Configuration();
cfg.configure();
cfg.addFile("test/mapping.xml");

SchemaExport se = new SchemaExport(cfg);
se.create(false, true);


Then I save an object Holder with its child and grandchildren:

Configuration cfg = new Configuration();
cfg.configure();
cfg.addFile("test/mapping.xml");

Holder h = new Holder("1");
h.con1 = new Container1(2);
Container2 con2_1 = new Container2("2");
h.con1.listCon2.add(con2_1);
            
sf = cfg.buildSessionFactory();
s = sf.openSession();
Transaction t = s.beginTransaction();
s.saveOrUpdate(con2_1);
s.saveOrUpdate(h);
t.commit();
s.close();


The db is set properly, with one row for the instance of the Holder class and one row for the instance of the Container2 class, with fk properly set to point to Holder's pk.
So, I try to load back objects from table Holder with this query:

Configuration cfg = new Configuration();
cfg.configure();
cfg.addFile("test/mapping.xml");
sf = cfg.buildSessionFactory();
s = sf.openSession();
            
Query q = s.createQuery("select h.con1 from testPZ.Holder h");
List l = q.list();

System.out.println(((Container1) l.iterator().next()).getListCon2());


I expected it to be properly set to the object retrieved from Container2 table but the outcome is an ArrayIndexOutOfBoundsException.



STACKTRACE:

java.lang.ArrayIndexOutOfBoundsException: 0
	at org.hibernate.hql.ast.util.ColumnHelper.generateScalarColumns(ColumnHelper.java:39)
	at org.hibernate.hql.ast.tree.DotNode.setScalarColumnText(DotNode.java:617)
	at org.hibernate.hql.ast.tree.SelectClause.renderScalarSelects(SelectClause.java:354)
	at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:228)
	at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:705)
	at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:529)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)
	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 testPZ.Main.main(Main.java:40)
---

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list