Reputation: 11946
I'm debugging the following function:
public List<ImpPeriode> importPeriode(Integer type, Date dateDebut) {
String sQuery = "from "+ImpPeriode.class.getCanonicalName()+" ip where ip.type = :type and ip.dateDebut > :dateDebut";
Query query = getSession()
.createQuery(sQuery)
.setParameter("type", type)
.setParameter("dateDebut", dateDebut);
return (List<ImpPeriode>) query.list();
}
The mapping for my ImpPeriode class is:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="importeloi">
<class name="com.an.eloi.model.domaine.importation.ImpPeriode" table="imp_periode">
<id name="id" type="java.lang.Integer">
<column name="periode_id" />
</id>
<property name="type" type="java.lang.Integer">
<column name="periode_type" not-null="true"/>
</property>
<property name="dateDebut" type="java.util.Date">
<column name="periode_datedeb" length="19" not-null="true"/>
</property>
<property name="dateFin" type="java.util.Date">
<column name="periode_datefin" length="19" not-null="true"/>
</property>
</class>
</hibernate-mapping>
but somehow, I keep getting an error indicating an error in my parameter naming:
org.hibernate.QueryParameterException: could not locate named parameter [type]
I'm using Java 7 and Hibernate 3.6.10.
Obviously I'm missing something, but what?
Edit:
I changed the code in order to make the word "type" disappear:
public List<ImpPeriode> importPeriode(Integer periodeType, Date periodeDateDebut) {
String sQuery = "from "+ImpPeriode.class.getCanonicalName()+" ip where ip.periodeType = :periodeType and ip.periodeDateDebut > :periodeDateDebut";
Query query = getSession()
.createQuery(sQuery)
.setParameter("periodeType", periodeType)
.setParameter("periodeDateDebut", periodeDateDebut);
return (List<ImpPeriode>) query.list();
}
and
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="importeloi">
<class name="com.an.eloi.model.domaine.importation.ImpPeriode" table="imp_periode">
<id name="periodeId" type="java.lang.Integer">
<column name="periode_id" />
</id>
<property name="periodeType" type="java.lang.Integer">
<column name="periode_type" not-null="true"/>
</property>
<property name="periodeDateDebut" type="java.util.Date">
<column name="periode_datedeb" length="19" not-null="true"/>
</property>
<property name="periodeDateFin" type="java.util.Date">
<column name="periode_datefin" length="19" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Sadly, I get exaclty the same error.
Upvotes: 0
Views: 77
Reputation: 11946
I forgot to declare the mapping file in the Spring application context. I knew it would be a silly memory lapse.
I have to say: the thrown exception and error message have nothing to do with the actual problem. It's really confusing.
Upvotes: 0
Reputation: 437
Type is a reserved word in HQL. If you rename your field it should work.
Upvotes: 1