Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
When I try to query on an "any" association (i.e. find all objects with a certain object in one of its fields, and that field is a java interface), I get an error from inside postgres complaining that too many columns are being bound. It is very easy to reproduce with just a little code, all of which is included below. Can anyone help me get past this? I have to use an any here; I have many classes that implement many different interfaces and this is the only way to map the associations.
Hibernate version: 3.2
Mapping documents:
Code:
==Container.hbm.xml:
<?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 default-access="field">
<class name="Container" table="Container">
<id name="hibernateId" column="container_id">
<generator class="native"/>
</id>
<any name="interf" meta-type="string" id-type="long">
<column name="interf_type"/>
<column name="interf_id"/>
</any>
</class>
</hibernate-mapping>
===Implementer.hbm.xml:
<?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 default-access="field">
<class name="Implementer" table="Implementer">
<id name="hibernateId" column="implementer_id">
<generator class="native"/>
</id>
<property name="value"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
import java.util.*;
import org.hibernate.*;
public class Test {
public static void main(String[] args) {
System.out.println(":::Isolation level is " +
System
.getProperty("hibernate.connection.isolation"));
new Test().create();
HibernateUtil.sessionFactory.close();
}
public void create() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Container container = new Container();
session.save(container);
Implementer implementer = new Implementer();
session.save(implementer);
container.setInterface(implementer);
tx.commit();
tx = session.beginTransaction();
Interface interf =
(Interface)
HibernateUtil.currentSession()
.createQuery("from Interface")
.list().iterator().next();
System.out.println(":::Interface found======");
interf.callOut();
Iterator qqq =
HibernateUtil.currentSession()
.createQuery("from Container where interf = :i")
.setParameter("i", interf)
.list().iterator(); //FAILS IN THE LIST CALL
while (qqq.hasNext()) {
Container c = (Container)qqq.next();
c.callOut();
}
tx.commit();
HibernateUtil.closeSession();
}
}
public class Container {
public Container() {
}
public void setInterface(Interface i) {
this.interf = i;
}
public void callOut() {
System.out.println("Container:");
System.out.print(" ");
interf.callOut();
}
private long hibernateId;
private Interface interf;
}
public interface Interface {
public void callOut();
}
public class Implementer implements Interface {
public Implementer() {
value = (int)(Math.random()*100);
}
public void callOut() {
System.out.println(":::Implementer #"+ value);
}
private int value;
private long hibernateId;
}
Full stack trace of any exception that occurs:Code:
10:24:12,682 INFO LongType:91 - could not bind value '16' to parameter: 2; The column index is out of range: 2, number of columns: 1.
10:24:12,694 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 22023
10:24:12,699 ERROR JDBCExceptionReporter:72 - The column index is out of range: 2, number of columns: 1.
Exception in thread "main" org.hibernate.exception.DataException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2148)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:392)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:333)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1123)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at Test.create(Test.java:31)
at Test.main(Test.java:10)
Caused by: org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:38)
at org.postgresql.core.v3.SimpleParameterList.setLiteralParameter(SimpleParameterList.java:68)
at org.postgresql.jdbc2.AbstractJdbc2Statement.bindLiteral(AbstractJdbc2Statement.java:2046)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setLong(AbstractJdbc2Statement.java:1088)
at org.hibernate.type.LongType.set(LongType.java:42)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.type.AnyType.nullSafeSet(AnyType.java:139)
at org.hibernate.type.AnyType.nullSafeSet(AnyType.java:117)
at org.hibernate.loader.hql.QueryLoader.bindNamedParameters(QueryLoader.java:515)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
... 9 more
Name and version of the database you are using: PostgreSQL 8.0.3
The generated SQL (show_sql=true):
select container0_.container_id as container1_0_, container0_.interf_type as interf2_0_, container0_.interf_id as interf3_0_ from Container container0_ where (container0_.interf_type, container0_.interf_id)=?
Debug level Hibernate log excerpt:
eh?