-->
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.  [ 5 posts ] 
Author Message
 Post subject: Expected positional parameter count
PostPosted: Wed Jun 28, 2006 2:34 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
What means the following exception?

org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [br.com.biblioshop.marc.domain.Campo@a79a6a] [from Subcampo where campo = ?]

Hibernate version: 3.1.3

Mapping documents:

<hibernate-mapping>

<class name="br.com.biblioshop.marc.domain.Subcampo" table="bs_mrc_sub">

<composite-id>
<key-many-to-one name="campo" class="br.com.biblioshop.marc.domain.Campo">
<column name="cod_base"/>
<column name="tag"/>
</key-many-to-one>
<key-property name="subcampo" column="sub"/>
</composite-id>

<property name="descricao">
<column name="descricao" length="50" not-null="true"/>
</property>

<property name="repetitivo">
<column name="repetitivo" not-null="true"/>
</property>

<property name="permiteBranco">
<column name="permite_branco" not-null="true"/>
</property>

</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

public List<Subcampo> listaPorCampo(Campo campo) {
return getHibernateTemplate().find("from Subcampo where campo = ?", campo);
}

Full stack trace of any exception that occurs:

Caused by: org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [br.com.biblioshop.marc.domain.Campo@a79a6a] [from Subcampo where campo = ?]
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:312)
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:75)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:766)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:315)
... 76 more

Name and version of the database you are using: MySQL 4.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 4:05 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
This is an issue where you couldnt use hql with composite id's. Instead use any other approach like direct sql's or individual props or criteria queries.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:00 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Just a guess, does your Campo class implement Serializable? From 6.1.5 of the Reference Guide:

Quote:
Your persistent class must override equals() and hashCode() to implement composite identifier equality. It
must also implements Serializable.


I've got a composite key class that looks like this:

Code:
package empcontact;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class EmpId implements Serializable {
    private int employeeId;
    private int departmentId;
   
    /**
     * Creates a new instance of EmpId
     */
    public EmpId() {
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public int getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).
                append(getEmployeeId()).
                append(getDepartmentId()).toString();
    }
   
    public boolean equals(Object obj) {
        if (obj instanceof EmpId == false) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        EmpId rhs = (EmpId) obj;
        return new EqualsBuilder().
                appendSuper(super.equals(obj)).
                append(getEmployeeId(), rhs.getEmployeeId()).
                append(getDepartmentId(), rhs.getDepartmentId()).
                isEquals();
    }

   public int hashCode() {
     return new HashCodeBuilder(17, 37).
       append(getEmployeeId()).
       append(getDepartmentId()).
       toHashCode();
   }
   
}


If I take out implements Serializable, I don't get your exact error, but my Hibernate code will throw an Exception like this:

Quote:
Exception in thread "main" java.lang.ClassCastException: empcontact.EmpId
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:759)


Hope this helps,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:33 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
My simple example code works fine with HSql and a composite key. The relevant lines look like:

Code:
    public Employee getCertainEmployee(EmpId empId) {
        return (Employee) getHibernateTemplate().get("empcontact.Employee", empId);
    }


There might be a restriction in more complex cases?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 6:56 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
Yes, my class implements Serializable.

This is the solution that I found:

Code:
getHibernateTemplate().find("from Subcampo where campo.base = ? and campo.tag = ?", new Object[] {campo.getBase(), campo.getTag()});


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