Techspace

IT happens only in IT

org.hibernate.QueryParameterException: could not locate named parameter

When you query the database in Hibernate using NamedQuery you may run into this error

org.hibernate.QueryParameterException: could not locate named parameter [parm1]

Chances are that you are either trying to set a parmeter to the query which infact doesn’t exist in your query. For example

you are trying to do

query.setParameter(“parm1″, new Long(parm1Value));

whereas, in query there is no parameter like parm1

i.e. query might be just “from Item item”

Or you might be committing some spelling mistake in your code

Eg. query.setParameter(“pram1″, new Long(parm1Value));

and query is “from Item item item.desc like :parm1″

Do note that your parameter name in Java Class is misspelt as pram1 instead of parm1.

January 8, 2008 Posted by | Hibernate | , , | 4 Comments

Hibernate Error – Identifier of an instance altered from 1 to 1

Problem:-Getting the error ” identifier of an instance of com.sample.db.Sample altered from 1 to 1″

Solution:- Check whether the java type of the identifier field in the Data Object, the type of the field in the hibernate xml file and the type of the column in the database are compatible types or not.

Explanation:- I had faced this strange problem today. I am using struts, hibernate and spring.

I found out that the problem roots from

public void

checkId(Object object, EntityPersister persister, Serializable id, EntityMode entityMode) method of DefaultFlushEntityEventListener class.

I solved this problem by changing the type of the id field in the class and the hbm.xml file.

We made some mistakes in declaring getter and setter methods and id field for this object. (therefore it is advisable to use Hibernate tools or other reverse engineer tools to generate your data object and hbm files)

The sample class was as follows

class Sample{

integer sampleId;

String sampleName;

public int getSampleId(){

….

}

public void setSampleId(int i){

…..

}

}

And this was the hbm file declaration

<id
name=”sampleId”
type=”integer”
column=”sample_id”
unsaved-value=”0″>
<generator class=”identity” />
</id>

<property
name=”sampleName”
type=”string”
column=”sample_name”
length=”50″
not-null=”true”
unique=”true”
/>

The problem was that in the database the type of the field was smallint which corresponds to the short data type in Java. Therefore somewhere in the org.hibernate.type.Type class hierarchy the

public boolean isEqual(Object x, Object y, EntityMode entityMode) method was failing.

What I did is I modified the Data Object and the hbm file like this

class Sample{

integer sampleId;

String sampleName;

public int getSampleId(){

public short getSampleId(){

….

}

public void setSampleId(int i){

public void setSampleId(short s){

…..

}

}

<id
name=”sampleId”
type=”integer”

type=”short”

column=”sample_id”
unsaved-value=”0″>
<generator class=”identity” />
</id>

May 16, 2007 Posted by | Hibernate | 14 Comments

/hibernate.cfg.xml not found

Problem: Many a times we get a problem in hibernate saying /hibernate.cfg.xml not found

Solution: As per Java Persistence with Hibernate book, you should keep the hibernate configuration file directly in the source directory of your project outside any package. In Rational Application Developer, I have solved this problem by placing it directly src folder.

March 18, 2007 Posted by | Hibernate | 9 Comments

Follow

Get every new post delivered to your Inbox.