Whats wrong with this Hibernate question?
Hello!I have implemented Hibernate as a databas layer and every thing seems to work well.
But I have some problems with my hql-statements. Hopefully you guys could give me a hint.
I got a table like:
/*
+-------------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nUserID | int(11) | NO | MUL | | |
| mobilephonenumber | char(50) | NO | | | |
+-------------------+----------+------+-----+---------+----------------+
*/
This table is mapped to a class like:
package model.hibernate;
public class Product
{
private Long id;
private Long nUserID;
private String mobilephonenumber;
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public Long getnUserID()
{
return nUserID;
}
public void setnUserID(Long nUserID)
{
this.nUserID = nUserID;
}
public String getMobilephonenumber()
{
return mobilephonenumber;
}
public void setMobilephonenumber(String a)
{
mobilephonenumber = a;
}
}
It is mapped like:
<?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>
<class name="model.hibernate.Product" table="products">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="nUserID"/>
<property name="mobilephonenumber"/>
</class>
</hibernate-mapping>
It is also registerd in the hibernate.cfg.xml like:
<mapping resource="attentionizer/model/hibernate/Product.hbm.xml"/>
I tries to create a query like:
Object[] parameters = {customer.getId()};
Collection products = CollectionService.getCollection("from Product where Product.nUserID = ?", parameters );
The code for the query looks like:
public static synchronized Collection getCollection(String hsql, Object[] parameters)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Query query = HibernateUtil.getSessionFactory().getCurrentSession().createQuery(hsql);
if(parameters != null)
{
for(int i = 0; i < parameters.length; i++)
{
query.setParameter(i, parameters[i]);
}
}
List result = query.list();
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
HibernateUtil.getSessionFactory().close();
return result;
}
But I just get this error:
500 Internal Server Error
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'Product.nUserID' [from model.hibernate.Product where Product.nUserID = ?]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:223)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:156)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:103)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:473)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1060)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1010)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at tools.CollectionService.getCollection(CollectionService.java:36)
So if any one could help me out it would be great!
Best regards
Fredrik
